无法从 Android AppWidgetProvider 启动 ActivityManager.RunningTaskInfo.topActivity
我有 MyAppWidgetProvider 类的 onUpdate(...) 方法和代码:
...
Intent launchAppIntent = new Intent(context, SearchActivity.class);
PendingIntent launchAppPendingIntent = PendingIntent.getActivity(context, 0, launchAppIntent, 0);
remoteView.setOnClickPendingIntent(R.id.logo, launchAppPendingIntent);
...
它工作正常。新的 SearchActivity 通过单击小部件启动。现在,我想开始单击而不是 SearchActivity,而是单击任务堆栈顶部的活动。为此,我将上面的代码更改为:
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(30);
for (ActivityManager.RunningTaskInfo task:tasks) {
if (task.baseActivity.getShortClassName().equals(".MainActivity")) {
try {
Intent launchAppIntent = new Intent(context, Class.forName(task.topActivity.getClassName()));
PendingIntent launchAppPendingIntent = PendingIntent.getActivity(context, 0, launchAppIntent, 0);
remoteView.setOnClickPendingIntent(R.id.logo, launchAppPendingIntent);
} catch (ClassNotFoundException ex) {
Logger.getLogger(CashWidgetProvider.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
这不起作用。任务堆栈中的前台活动不会出现在监视器上。此外,我在运行此代码的日志中没有收到任何消息。
但是,相同的代码可以很好地放置在从 Activity 继承的类的某些方法中。
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(30);
for (ActivityManager.RunningTaskInfo task:tasks) {
System.out.println(task.baseActivity.getShortClassName());
if (task.baseActivity.getShortClassName().equals(".MainActivity")) {
try {
Intent launchAppIntent = new Intent(this, Class.forName(task.topActivity.getClassName()));
startActivity(launchAppIntent);
} catch (ClassNotFoundException ex) {
Logger.getLogger(AbstractActivity.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
我怎样才能让它在 AppWidget 上工作!?
I have onUpdate(...) metod of MyAppWidgetProvider class with the code:
...
Intent launchAppIntent = new Intent(context, SearchActivity.class);
PendingIntent launchAppPendingIntent = PendingIntent.getActivity(context, 0, launchAppIntent, 0);
remoteView.setOnClickPendingIntent(R.id.logo, launchAppPendingIntent);
...
It works fine. New SearchActivity starts by clicking on widget. Now, I want to start on click not the SearchActivity, but an activity from the top of the stack of my task. For this purpose I change above code to:
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(30);
for (ActivityManager.RunningTaskInfo task:tasks) {
if (task.baseActivity.getShortClassName().equals(".MainActivity")) {
try {
Intent launchAppIntent = new Intent(context, Class.forName(task.topActivity.getClassName()));
PendingIntent launchAppPendingIntent = PendingIntent.getActivity(context, 0, launchAppIntent, 0);
remoteView.setOnClickPendingIntent(R.id.logo, launchAppPendingIntent);
} catch (ClassNotFoundException ex) {
Logger.getLogger(CashWidgetProvider.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
that doesn't work. Foreground activity in task stack doesn't appear on monitor. Moreover, I don't get any messages in log running this code.
However, the same code works fine being placed in some method of the class, inherited from Activity.
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(30);
for (ActivityManager.RunningTaskInfo task:tasks) {
System.out.println(task.baseActivity.getShortClassName());
if (task.baseActivity.getShortClassName().equals(".MainActivity")) {
try {
Intent launchAppIntent = new Intent(this, Class.forName(task.topActivity.getClassName()));
startActivity(launchAppIntent);
} catch (ClassNotFoundException ex) {
Logger.getLogger(AbstractActivity.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
How can I get it working for AppWidget!?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像往常一样,问题的出现是由于对小部件生命周期的误解......为了使其正常工作,我必须创建从 Service 继承的 MyService 类,并使用代码覆盖 onStart 方法:
并通过代码从 AppWidgetProvider 类调用上面的 MyService
: 部分下添加此新服务。
忘记在 AndroidManifest.xml 中的P.S. 也许,使用 BroadcastReceiver 而不是 Service 会更好。
The problem appears due to misunderstanding of widget lifecycle, as usual... To get it working I have to create MyService class inherited from Service overriding onStart method with code:
and call above MyService from AppWidgetProvider class by means of the code:
Don't forget to add this new service in AndroidManifest.xml under section
P.S. Probably, it would be better to use BroadcastReceiver instead of Service.