-->

Starting an Activity from Android Wear Watchface

2019-07-31 04:58发布

问题:

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:

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)

;