Starting an Activity from Android Wear Watchface

人走茶凉 提交于 2019-12-11 07:56:25

问题


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)

;



来源:https://stackoverflow.com/questions/47960014/starting-an-activity-from-android-wear-watchface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!