java - 从服务启动应用程序

标签 java android service android-intent

好的,这是我的问题。我有一个接收推送通知的服务。现在,根据推送通知的内容,如果单击它,它应该启动相应的应用程序。

我在接收推送通知或使用该服务时没有任何问题,我唯一无法工作的是它打开正确的应用程序的部分。

我需要做什么才能从服务打开应用程序?这必须是动态的,因为将有多个应用程序使用此服务。

如果有人能指出我正确的方向,那就太好了。

PS。我正在使用 GCM 服务,我将其放入库中,因此我可以在我的多个应用程序中使用它。这是 GCMItentService.onMessage() 函数,我需要在其中检查 url 的内容,然后为通知设置正确的 Intent :

@Override
protected void onMessage(Context arg0, Intent arg1)
{
    Log.i(TAG, "new message = " + arg1.getExtras());

    //Get a reference to the NotificationManager
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

    //Instantiate the Notification      
    int icon = getResourseIdByName(arg0.getApplicationContext().getPackageName(), "drawable", "notification_icon");
    CharSequence tickerText = arg1.getExtras().getString("tickertext");
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);

    //Define the notification's message and PendingIntent 
    Context context = getApplicationContext();
    CharSequence contentTitle = arg1.getExtras().getString("contentTitle");
    CharSequence contentText = arg1.getExtras().getString("contentText");

    Intent notificationIntent = null;

    if(arg1.getExtras().getString("url").isEmpty())
    {
        notificationIntent = new Intent(this, arg1.getExtras().getString("packageName").getClass());
    }
    else
    {
        notificationIntent = new Intent(this, MyWebView.class);
    }

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    notification.flags = Notification.FLAG_AUTO_CANCEL;

    //Pass the Notification to the NotificationManager
    int notificationId = Integer.parseInt(arg1.getExtras().getString("notificationId"));

    mNotificationManager.notify(notificationId, notification);
}

最佳答案

可以使用Intent启动应用程序。您需要构造正确的 PendingIntent 对象,如下所示:

Intent targetIntent = new Intent(Intent.ACTION_MAIN, null);
targetIntent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName("target_app_package_name", "target_activity_class_name");
targetIntent.setComponent(cn);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, targetIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

如果您不知道目标包或目标Activity,您可以使用以下代码:

Intent startingIntent = context.getPackageManager().getLaunchIntentForPackage("target_app_package_name");
context.startActivity(startingIntent);

关于java - 从服务启动应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12971611/

相关文章:

java - 在 Java 1.7 中连同元素一起从数组中删除重复元素

java - Android/Arduino蓝牙通讯

java - struts2 中的paramsPrepareParamsStack 拦截器?

android - 从 Activity 绑定(bind)到服务或在不同进程中启动服务?

c# - 如何使用 c# 以编程方式安装系统服务以使用组托管服务帐户 (gMSA)?

c# - 在 http//上没有监听端点可以接受 WCF 中的消息

java - 如何使用java Stream检查集合是否为空

android - 处理更改的 ViewPager fragment

Proxy 后面的 Android Lollipop WebView 将无法工作

android - 如何在 android studio 1.5.x 中使用选项卡式 Activity ?