首页 > 移动平台 > 详细

Android 开机自启动示例程序

时间:2015-11-01 10:12:55      阅读:406      评论:0      收藏:0      [点我收藏+]

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;

	@Override
	public 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 {

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return 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;
	@Override
	protected 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() {
			@Override
			public void onClick(View v) {
			    Editor editor = mPreferences.edit();
			    editor.putBoolean("AddToAuto", true);
			    editor.commit();
			    textView1.setText("已打开开机自启动");
				
			}
		});
		
		//关闭
		findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
			    Editor editor = mPreferences.edit();
			    editor.putBoolean("AddToAuto", false);
			    editor.commit();
			    textView1.setText("已关闭开机自启动");
				
			}
		});
		
	}

	@Override
	public 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 开机自启动示例程序

原文:http://blog.csdn.net/qq_16064871/article/details/49555991

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!