简体   繁体   English

隐含服务启动主要活动

[英]Launch Main Activity with Implicit Intent in Service

I have an implicit intent in a service that sends information to my main activity, as well as to another class. 我在将信息发送到我的主要活动以及另一个类的服务中有一个隐式意图。 I also now want that intent to launch my main activity. 我现在也希望该意图启动我的主要活动。 I've looked at the myriad posts related to this, and tried lots of different things-- addCategory , setAction(MAIN; the activity's name; you name it, I've tried it...) , category.DEFAULT in the manifest, and several other things that either resulted in ActivityNotFoundExceptions (most commonly) or behavior that was otherwise undesirable. 我查看了与此相关的大量文章,并尝试了许多不同的方法addCategorysetAction(MAIN; the activity's name; you name it, I've tried it...) ,清单中的category.DEFAULT ,以及导致ActivityNotFoundExceptions(最常见)或其他不希望有的行为的其他几种情况。

Here's where the intent is set up and the relevant part of the manifest. 在这里设置意图和清单的相关部分。 The receiver for the intent is registered in the main activity. 意图的接收者已在主要活动中注册。

final String NEW_DOSES = "changed to protect the innocent";
Intent bluetoothBroadcast = new Intent();
several putExtra lines here
bluetoothBroadcast.setAction(NEW_DOSES);
sendBroadcast(bluetoothBroadcast);

<activity
   android:name=".AsthmaAppActivity"
   android:label="@string/app_name" >
   <intent-filter>
       <action android:name="android.intent.action.MAIN" />
       <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
</activity>

Is it possible to get this intent to launch my main activity with relatively minor changes? 是否可以通过较小的更改来启动我的主要活动? Thanks in advance. 提前致谢。

Yes it is possible but no with sendBroadcast(bluetoothBroadcast); 是的,可以,但sendBroadcast(bluetoothBroadcast)不能。 sendBroadcast does not launch an activity. sendBroadcast不启动活动。 You must use startActivity to achieve this. 您必须使用startActivity来实现。 For example here is what a launcher application will do in order to launch an application: 例如,这是启动器应用程序将要启动的应用程序的工作:

public static void LaunchApplication(Context cx, String packagename) {
    PackageManager pm = cx.getPackageManager();
    Intent i = pm.getLaunchIntentForPackage(ai.packageName);
    if (i != null) cx.startActivity(i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}

You can easily adjust the extras and the data needed in order to launch the activity. 您可以轻松调整额外功能和所需数据以启动活动。 For example if your activity is named myActivity then you can go like this: 例如,如果您的活动名为myActivity,则可以这样进行:

Intent i = new Intent(cx, myActivity.class);
//Put the extras and the data you want here...
//If you are launching the activity from a receiver component you must use
//i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
cx.startActivity(i);

Hope this helps... 希望这可以帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM