【问题标题】:Android: Launch an external app from notificationAndroid:从通知启动外部应用程序
【发布时间】:2014-03-11 00:12:37
【问题描述】:

我正在尝试创建一个通知,单击该通知将打开一个外部应用程序。我看过creating notificationssending the user to another app 的文档。但我似乎无法弄清楚如何将两者结合起来。问题是从通知启动应用程序的建议方法是创建 待定意图,如下所示:

Intent intent = new Intent(this, MyActivity.class);
TaskStackBuilder stackBuidler = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MyActivity.class); 
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

但是要启动一个外部应用程序,您必须像这样创建一个隐式意图

String uri = ...
Intent intent = new Intent(Intent.ACTION_VIEW, uri);

据我所知,没有办法以这种意图创建 TaskStackBuilder,因为addParentStack() 只会采用ActivityClassComponentName

我想问题归结为...是否可以创建一个既未决又隐含的意图?

我现在能想到的唯一解决方法是在我的应用中创建一个除了启动外部应用之外什么都不做的 Activity。

我确实尝试从 URI 创建意图,然后执行以下操作,但是当您单击通知时没有任何反应:

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

【问题讨论】:

    标签: android android-intent android-notifications


    【解决方案1】:

    好吧,很久以后,我想我已经找到了适合你的解决方案。 对于所有其他正在寻找从您自己的自定义通知启动外部应用程序的答案的人。 这里是:

    public void createMyNotification(String titl, String titlcont, String conti){
            Context context = getApplicationContext();
            PackageManager pm = context.getPackageManager();
            Intent LaunchIntent = null;
            String apppack = "com.mycompany.appack.apname.app";
            String name = "";
            try {
                if (pm != null) {
                    ApplicationInfo app = context.getPackageManager().getApplicationInfo(apppack, 0);
                    name = (String) pm.getApplicationLabel(app);
                    LaunchIntent = pm.getLaunchIntentForPackage(apppack);
                }
                Toast.makeText(context,"Found it:" + name,Toast.LENGTH_SHORT).show();
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
    
            Intent intent = LaunchIntent; // new Intent();
            PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
            Notification noti = new Notification.Builder(this)
                    .setTicker(titl)
                    .setContentTitle(titlcont)
                    .setContentText(conti)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentIntent(pIntent).getNotification();
            noti.flags = Notification.FLAG_AUTO_CANCEL;
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.notify(0, noti);
        }
    

    这不会使用额外的 Activity 来启动外部应用程序。

    【讨论】:

    • 非常感谢您对我的帮助。
    【解决方案2】:

    如果你知道你想要启动它的 packageName,你可以得到 startActivity 的 Intent。
    阅读此链接start application knowing package name

    【讨论】:

    • 这不能回答我的问题。我已经知道如何创建启动意图以打开另一个应用程序,但我不知道如何将其转换为 PendingIntent 以便它可以附加到通知中。文档中的示例使用 TaskStackBuilder,但仅适用于内部活动。
    【解决方案3】:

    我不知道这是否是最好的方法,但这是最终奏效的解决方案:

    创建通知

    //Create the pending intent
    Intent intent = new Intent(context, MyActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(MyActivity.class);
    stackBuilder.addNextIntent(intent);
    PendingIndent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    
    //Create the notification builder
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
        .setContentTitle("Notification Title")
        .setContentText("Hello world!")
        .setContentIntent(pendingIntent); //Attach the pending intent to launch when notification is clicked
    
    //Send the notification
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(mId, builder.build());
    

    MyActivity.java

    public class MyActivity extends Activity {
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            //Create implicit intent - see http://developer.android.com/training/basics/intents/sending.html
            String uri = "...";
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    
            startActivity(intent);
        }
    }
    

    我仍然很想知道是否有办法绕过额外的 Activity,它只会启动一个隐式 Intent。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多