Android 开机自启动示例程序。使用广播方式接受,采用Android自带存储SharedPreferences存储开机自启动的设置。

本文源码:点击

1、先加上权限

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

2、需要的广播接收注册(如果还要启动服务,也先注册)

        <!-- 开机自启动广播接受 -->        <receiver android:name="com.example.autostart.AutoStartBroadcastReceiver" >            <intent-filter>                <action android:name="android.intent.action.BOOT_COMPLETED" />            </intent-filter>        </receiver>                <!-- 开机自启动服务-->         <service android:name="com.example.autostart.AutoStartService"            android:label="AutoStartService"            android:enabled="true"            android:exported="true"            android:process=":remote">        </service>

3、广播接收AutoStartBroadcastReceiver

package com.example.autostart;import android.content.BroadcastReceiver;import android.content.Context;import android.content.ContextWrapper;import android.content.Intent;import android.content.SharedPreferences;//开机自启动广播接受public class AutoStartBroadcastReceiver extends BroadcastReceiver {private static final String ACTION = "android.intent.action.BOOT_COMPLETED";private SharedPreferences mPreferences = null;@Overridepublic void onReceive(Context context, Intent intent) {mPreferences = context.getSharedPreferences("AutoStart",ContextWrapper.MODE_PRIVATE);if (intent.getAction().equals(ACTION)) {if (mPreferences.getBoolean("AddToAuto", false)) {    //后边的XXX.class就是要启动的服务          Intent service = new Intent(context,AutoStartService.class);          context.startService(service);  // 启动应用,参数为需要自动启动的应用的包名,只是启动app的activity的包名Intent newIntent = context.getPackageManager().getLaunchIntentForPackage("com.example.autostart");context.startActivity(newIntent);}}}}

4、如果需要启动一些服务再写(可选项)

如果程序需要启动一些必要的服务再写这个也可以,一般开机自启动只需要启动app的主activity。这里示范一下写服务。

package com.example.autostart;import android.app.Service;import android.content.BroadcastReceiver;import android.content.Context;import android.content.ContextWrapper;import android.content.Intent;import android.content.SharedPreferences;import android.os.IBinder;import android.util.Log;//开机自启动广播接受public class AutoStartService extends Service {@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}    @Override    public void onCreate(){       super.onCreate();       Log.d("TAG2","test service"); }}

5、怎样使用这些配置MainActivity

package com.example.autostart;import android.os.Bundle;import android.app.Activity;import android.content.ContextWrapper;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.TextView;public class MainActivity extends Activity {private SharedPreferences mPreferences = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mPreferences = getSharedPreferences("AutoStart",ContextWrapper.MODE_PRIVATE);boolean bStart = mPreferences.getBoolean("AddToAuto", false);final TextView textView1 = (TextView)findViewById(R.id.textView1);if (bStart) {textView1.setText("已打开开机自启动");}else {textView1.setText("已关闭开机自启动");}//打开findViewById(R.id.button1).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {    Editor editor = mPreferences.edit();    editor.putBoolean("AddToAuto", true);    editor.commit();    textView1.setText("已打开开机自启动");}});//关闭findViewById(R.id.button2).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {    Editor editor = mPreferences.edit();    editor.putBoolean("AddToAuto", false);    editor.commit();    textView1.setText("已关闭开机自启动");}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);return true;}}

6、简单界面实现图

Android 开机自启动示例程序_第1张图片


到此结束,本文源码:点击

更多相关文章

  1. 【Android okhttp源码解析 四】任务调度核心类dispatcher解析
  2. Windows下用Git下载android源码
  3. Android状态栏适配源码解析。
  4. 如何使用arm-eabi-gdb调试android c/c++程序
  5. (一百三十八)学习Android studio 导入Android源码
  6. android Button源码分析
  7. 【Android okhttp源码解析 五】拦截器流程和源码解析
  8. Ubuntu 16.04环境下使用Clion 2019.1.4 gdb调试Android 7.1.2源
  9. android唤起另外一个程序

随机推荐

  1. 试图改变Jtable java中行的颜色
  2. idea下Kotlin的扁平化集合flatMap
  3. 使加权图在JGraphT中工作
  4. 如何将动态参数传递给jquery函数
  5. android中java.net.Socket的默认超时值是
  6. 使用Java管理Azure(1):基础配置
  7. java中 16进制字符串 与普通字符串 与 by
  8. java 构造器内部的多态方法和行为
  9. java I/O流初步认识使用
  10. JavaScript-C/C++ (SpiderMonkey) 引擎嵌