Android开机自启

public class BroadCast extends BroadcastReceiver {

private static final String Action="android.intent.action.BOOT_COMPLETED";

@Override
public void onReceive(Context contextIntent intent) {
if (intent.getAction().equals(Action))
{

// Intent MyScene=new Intent(context, com.slq.myapp.MyScene.class);
// MyScene.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Intent MyScene=context.getPackageManager().getLaunchIntentForPackage("com.slq.myapp");

context.startActivity(MyScene);
}

}

}   

一定要把BroadCast类建在com.Slq.xxx下边,不能建在Java目录下,否则的话会开机自启的时候出错。                                                                         

                                                                                                                                 

 

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.slq.myapp">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MyScene">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:name=".BroadCast">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>

</intent-filter>
</receiver>

</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
</manifest>  




猜你喜欢

转载自blog.csdn.net/qq_39353597/article/details/78362777
今日推荐