2016-07-20 52 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

相關問題