【问题标题】:Send intent from listview using a service使用服务从列表视图发送意图
【发布时间】:2012-03-12 16:52:29
【问题描述】:

我正在制作一个应用程序,它使用包管理器来查找手机上的所有应用程序并将其显示在列表视图中。现在它只打开应用程序,但我想使用服务和广播接收器将意图发送到另一个类。这个想法是创建一个快捷方式,通过单击另一个类中的按钮来启动选定的应用程序。这是我的代码:

protected void onListItemClick(ListView l, View v, int position, long id) {
ResolveInfo launchable=adapter.getItem(position);
ActivityInfo activity=launchable.activityInfo;
ComponentName name=new ComponentName(activity.applicationInfo.packageName,
                                     activity.name);
Intent i=new Intent(Intent.ACTION_MAIN);


i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
           Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
i.setComponent(name);

startActivity(i);    

} 我希望通过单击列表视图中的另一个应用程序来更改服务。我也希望它保存在手机上。

有没有办法做到这一点?

【问题讨论】:

    标签: android listview service android-intent broadcastreceiver


    【解决方案1】:

    我通过创建一个包含包名的 SharedPreference 解决了这个问题。通过用 try-catch 包围它,我设法让它在可能的情况下启动应用程序,否则不会崩溃。要“删除”选择,我只需将 Prefrence 更改为“”,或者不是包名。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      • 2014-08-04
      • 1970-01-01
      相关资源
      最近更新 更多