Method of setting Android code to start App automatically
- 2021-09-24 23:46:29
- OfStack
Sometimes you want the user to turn on the mobile phone once. Our APP runs automatically.
The code is as follows:
Create 1 listener.
/**
* create by : sunlei on 2017/7/7 15:48
* e-mail : 872822645@qq.com
* introduce:
*/
public class ContentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent it=new Intent(context,MainActivity.class);
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(it);
Toast.makeText(context," I started myself successfully, huh ",Toast.LENGTH_LONG).show();
}
}
Note: If it is a page jump. flags must be added here.
Add permissions and register this broadcast in the configuration file:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".ContentReceiver"
>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
This broadcast is registered here. Used to monitor. . Two category are home and launcher. . Choose 1 from 2
Finally, pay attention. Most mobile phones have butler software, which restricts self-startup. . So if it doesn't work. Allow needs to be set.
I used Xiaomi NOTE mobile phone test. LeTV 2 mobile phone. Pro-test is effective!