Android:如何通过我的通知打开其他应用?

时间:2016-07-20 20:56:31

标签: android notifications

我一直在尝试如何为我的应用程序发出通知,打开另一个应用程序,例如收件箱。我只看到了如何打开特定活动,并且不知道是否可以打开整个应用程序。

2 个答案:

答案 0 :(得分:1)

是。有可能的。您只需要正确配置您的意图。

注意

最终用户可能没有安装你想要的应用程序。所以,你必须实现控制它的方法......

但无论如何,可以从您自己的通知中打开另一个应用

示例

我为whatsapp创建了以下示例。我使用this question作为参考。

Notification.Builder notiBuilder = new Notification.Builder(this);
Intent intent = null;

/*
    START
    Configure your intent here.
    Example below opens the whatspp.. I got this example from https://stackoverflow.com/questions/15462874/sending-message-through-whatsapp/15931345#15931345
    You must update it to open the app that you want.

    If the app is not found, intent is null and then, click in notification won't do anything
*/
PackageManager pm=getPackageManager();
try {
    PackageInfo info = pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
    intent = new Intent(Intent.ACTION_SEND);
    intent.setPackage("com.whatsapp");
    intent.setType("text/plain");
} catch (PackageManager.NameNotFoundException e) {
    // Package not found
    intent = null;
    e.printStackTrace();
}
/*  END  */

if(intent != null) {
    PendingIntent clickPendingIntent = PendingIntent.getActivity(
            this,
            0,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notiBuilder.setContentTitle("Title")
            .setSmallIcon(R.drawable.common_google_signin_btn_icon_light)
            .setContentText("Message")
            .setContentIntent(clickPendingIntent)
            .setLights(Color.BLUE, 3000, 3000);
} else {
    notiBuilder.setContentTitle("Title")
            .setSmallIcon(R.drawable.common_google_signin_btn_icon_light)
            .setContentText("Message")
            .setLights(Color.BLUE, 3000, 3000);
}

Notification mNotificationBar = notiBuilder.build();
mNotificationBar.flags |= Notification.DEFAULT_SOUND;
mNotificationBar.flags |= Notification.FLAG_SHOW_LIGHTS;
mNotificationBar.flags |= Notification.FLAG_AUTO_CANCEL;

NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Service.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mNotificationBar);

打开拨号器

只需配置如下的意图:

intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:0123456789"));

答案 1 :(得分:0)

这是关于如何创建隐含意图的官方文档。

https://developer.android.com/training/basics/intents/sending.html

这是你的意思吗?