Android:从通知中启动外部应用程序

时间:2014-03-11 00:12:38

标签: android android-intent android-notifications

我正在尝试创建一个通知,单击该按钮将打开外部应用程序。我见过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()只会使用ActivityClass或{ {1}}。

我想这个问题归结为......是否有可能创建一个既有待处理又隐含的意图?

我现在能想到的唯一解决方法是在我的应用中创建一个只启动外部应用程序的活动。

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

ComponentName

3 个答案:

答案 0 :(得分:3)

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

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);
    }

这不会使用其他活动来启动外部应用程序。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

创建通知

//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);
    }
}

我仍然有兴趣知道是否有办法绕过一个额外的活动,除了发起一个隐含的意图之外什么都不做。