1, Set application launch mode in AndroidManifest.xml to implement single instance (More details about launchMode, please refer to
):
android:label="@string/app_name"
android:launchMode="singleInstance">
android:label="Main Window"
android:icon="@drawable/icon>
2, If you find single instance doesn't take effect when you install and run your application from Eclipse, please press
Run >
Run Configurations ...,and select the configuration item of your application on your Eclipse, then select
Do Nonthing in group box
Launch Action in
Android tab. (A launch configuration specifies the project to launch, the Activity to start, the emulator options to use, and so on, please refer to online document http://code.google.com/intl/en-CN/android/intro/develop-and-debug.html in
section Create a Launch Configuration to get more details)
3, If your application is not launched manually by clicking application's icon in Android applications list, ie, BroadcaseReceiver launches your application, you need to set correct action and category before launch application as following:
public class MyReceiver extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
//Phase 2: Launch an activity
Intent newIntent = new Intent(this, Main.class); //Activity name
newIntent.setAction("android.intent.action.MAIN"); //Activity action defined in AndroidManifest.xml
newIntent.setCategory("android.intent.category.LAUNCHER");//Activity category defined in AndroidManifest.xml
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(newIntent);
}
}
}
阅读(2574) | 评论(0) | 转发(0) |