Is it possible to launch an android application activity when the phone starts?
Im attempting to build an android application and one of the key features to this application is for开发者_如何学JAVA it to be able to launch an activity automatically when the phone starts, I see some apps on my phone that already do this, any help would be great so that I can atleast research this a little better through the sdk, thanks!
You need to implement BroadCastreceiver like this:
public class PhoneStateReceiver extends BroadcastReceiver{
@Override
public void onReceive(final Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Intent launch = new Intent(context, AcitivityToLaunch.class);
launch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(launch);
}
}
}
In your manifest add this:
<receiver android:name=".receiver.PhoneStateReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Add permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
精彩评论