无法从 Android AppWidgetProvider 启动 ActivityManager.RunningTaskInfo.topActivity

发布于 2024-12-11 17:38:21 字数 2122 浏览 1 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福%小乖 2024-12-18 17:38:21

像往常一样,问题的出现是由于对小部件生命周期的误解......为了使其正常工作,我必须创建从 Service 继承的 MyService 类,并使用代码覆盖 onStart 方法:

...
Context context = this.getApplicationContext();
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 launchTopIntent = new Intent(context, Class.forName(task.topActivity.getClassName()));
                context.startActivity(launchTopIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));             
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(CashWidgetProvider.class.getName()).log(Level.SEVERE, null, ex);
        }                  
    }
} 
...

并通过代码从 AppWidgetProvider 类调用上面的 MyService

Intent launchAppIntent = new Intent(context, MyService.class);
launchAppIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent launchAppPendingIntent = PendingIntent.getService(
            context.getApplicationContext(), 0, launchAppIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteView.setOnClickPendingIntent(R.id.logo, launchAppPendingIntent2); 

: 部分下添加此新服务。

<application>
    ...
    <service android:name=".MyService"/>
</application>

忘记在 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:

...
Context context = this.getApplicationContext();
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 launchTopIntent = new Intent(context, Class.forName(task.topActivity.getClassName()));
                context.startActivity(launchTopIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));             
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(CashWidgetProvider.class.getName()).log(Level.SEVERE, null, ex);
        }                  
    }
} 
...

and call above MyService from AppWidgetProvider class by means of the code:

Intent launchAppIntent = new Intent(context, MyService.class);
launchAppIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent launchAppPendingIntent = PendingIntent.getService(
            context.getApplicationContext(), 0, launchAppIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteView.setOnClickPendingIntent(R.id.logo, launchAppPendingIntent2); 

Don't forget to add this new service in AndroidManifest.xml under section

<application>
    ...
    <service android:name=".MyService"/>
</application>

P.S. Probably, it would be better to use BroadcastReceiver instead of Service.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文