问题
How do i set my application as startup application, so when ever mobile restarts or turned ON, my application starts.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.installedapps22"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application android:icon="@drawable/cherry_icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ListInstalledApps" > </activity>
<activity android:name=".TabsLayoutActivity" />
</application>
</manifest>
EDIT Here is my updated code and it is still not working:
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.installedapps22"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="@drawable/cherry_icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:enabled="true" android:name="com.app.reciever.BootUpReciever">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity android:name=".ListInstalledApps" > </activity>
<activity android:name=".TabsLayoutActivity" />
</application>
</manifest>
BroadcastReciever:
package com.example.installedapps22;
public class BootUpReciever extends BroadcastReceiver
{
@Override
public void onReceive(final Context context, Intent intent) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
回答1:
This is to set the app as the startup application in your device Create a Class extends BroadCast Reciever
public class BootUpReciever extends BroadcastReceiver
{
@Override
public void onReceive(final Context context, Intent intent) {
Intent i = new Intent(context, ServerPreferenceActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
Add permissions to manifest file to access bootup receiver
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Register your receiver which extended the Broadcast receiver
in manifest.xml
<receiver android:enabled="true" android:name="com.app.reciever.BootUpReciever">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
回答2:
first use permission in manifiest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and declare boot receiver in manifiest
<receiver android:name=".BootReciever">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
user receiver to start your mainactivity
public class BootReciever extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
回答3:
For your edited code try replacing the following:
android:name="com.app.reciever.BootUpReciever
with:
android:name="com.example.installedapps22.BootUpReciever
来源:https://stackoverflow.com/questions/17168177/how-to-start-my-application-when-ever-mobile-restart-or-turn-on