【问题标题】:Starting an application from a service从服务启动应用程序
【发布时间】:2012-10-19 09:47:44
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

    标签: java android service android-intent


    【解决方案1】:

    可以使用Intents 启动应用程序。您需要构造正确的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);
    

    【讨论】:

    • 现在可以使用了。如果我使用这个:notificationIntent = arg0.getPackageManager().getLaunchIntentForPackage(arg1.getExtras().getString("packageName").toString());
    【解决方案2】:

    嗯,看一眼就可以得到对应应用的完整包名。

    然后你可以通过包名启动应用程序,像这样

    Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.your.packageName.in.String");
    startActivity(LaunchIntent); 
    

    编辑:

    为此,AFAIK,您只能从创建该通知的您自己的包中启动一个活动。您可以从另一个应用程序包启动一个活动,但是您必须知道该包的活动类名称,而您通常不可能知道。

    Intent clickIntent = new Intent(context,Activity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context,0,clickIntent,0);
    

    这是您在创建通知时需要的。

    notification.setlatestEventInfo(context, "title","content",pendingIntent);
    

    啊!抱歉评论很糟糕...我似乎无法在 cmets 中输入代码:(

    【讨论】:

    • 但是我怎样才能把它放到推送通知中呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 2011-04-04
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多