2016-07-20 48 views
0

我一直在尝试如何为我的应用程序发出通知,以打开不同的应用程序,如收件箱。我只看到如何打开特定的活动,不知道是否可以打开整个应用程序。Android:如何通过我的通知打开其他应用程序?

+0

我认为这是可能的。你只需要正确配置意图...但我从来没有尝试过..所以,我无法确认 – W0rmH0le

+0

Android中没有“完整的应用程序”。这类似于询问如何从您的网页链接到“整个网站”。考虑到您知道与PackageManager一起使用的应用程序ID(“包名称”),您可以为应用程序启动启动器活动。由于并非所有人都使用Google Inbox,因此您需要一些方法让用户决定应该打开哪个应用程序。 – CommonsWare

回答

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:")); 
+0

很酷,这个帮了很多! – TCTBO

相关问题