Starting an Activity from Android Wear Watchface

2019-07-31 04:30发布

I am trying to start an activity from a watchface in Android Wear on a click using:

            Intent myIntent = new Intent(this, com.jorc.android.wearable.watchface.watchface.MainActivity.class);
            getApplicationContext().startActivity(myIntent);

However, I get this error.

Error:(564, 39) error: no suitable constructor found for Intent(DigitalWatchFaceService.Engine,Class<MainActivity>)
constructor Intent.Intent(String,Uri) is not applicable
(argument mismatch; DigitalWatchFaceService.Engine cannot be converted to String)
constructor Intent.Intent(Context,Class<?>) is not applicable
(argument mismatch; DigitalWatchFaceService.Engine cannot be converted to Context)

My question is: "Is there a way to start an activity from a watchface"? Watchface uses CanvasWatchFaceService.Engine which extends CanvasWatchFaceService.

1条回答
够拽才男人
2楼-- · 2019-07-31 05:07

There was a small missing part when using Intent myIntent = new Intent(this, ...MainActivity.class) MainActivity.class) The intent is created inside another class, here an anonymous inner class canvas click listener. The code does not refer the instance of the Activity (or Context) as intended but the instance of the anonymous inner class canvas ClickListener.

So the right way was to provide the correct context of class.

 Intent myIntent = new Intent(DigitalWatchFaceService.this, com.jorc.android.wearable.watchface.watchface.MainActivity.class);
 getApplicationContext().startActivity(myIntent)

;

查看更多
登录 后发表回答