Android--开机自启动(activity或service)

简介: 版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/78223243 Android手机在启动的过程中会触发一个Standard Broadcast Action,名字叫android.intent.action.BOOT_COMPLETED(记得只会触发一次呀),在这里我们可以通过构建一个广播接收者来接收这个这个action。
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/78223243

Android手机在启动的过程中会触发一个Standard Broadcast Action,名字叫android.intent.action.BOOT_COMPLETED(记得只会触发一次呀),在这里我们可以通过构建一个广播接收者来接收这个这个action。必须要注意的一点是:这个广播必须的静态注册的,不能是动态注册的广播(这种接受开机广播的,一定要静态注册,这样应用还没运行起来时也照样能够接收到开机广播  ,动态广播就不行了)。

在装上demo让demo运行后,先关机,再启动。也就是说装上应用运行后,一定要重启机器。

如果失败:看看有没有装360之类的被限制,还有手机自带的管理自启动的软件,进去点击允许;

加入权限:<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

运行方式: adb shell am startservice -n 包名/包命.TestService        //manifest中的service name

adb模拟开机:adb shell am broadcast -a android.intent.action.BOOT_COMPLETED

1、注册开机广播,在清单文件中注册:

<receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="1000">
                <action android:name="android.intent.action.BOOT_COMPLETED"></action>
            </intent-filter>
		</receiver>
		
        <service android:name="com.example.openandroid.MyService">
            <intent-filter >
               <action android:name="com.example.openandroid.MyService" />  //必须是全名
               <category android:name="android.intent.category.default" />
            </intent-filter>
        </service>

2.在开机广播的onReceive方法里面做跳转:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyReceiver extends BroadcastReceiver
{
    public MyReceiver()
    {
    }

    @Override
    public void onReceive(Context context, Intent intent)
    {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
        {
//            Intent i = new Intent(context, MainActivity.class);
//            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            context.startActivity(i);
        	Intent i = new Intent(context, MyService.class);
        	context.startService(i);
        }
    }

}

3.MainActivity

mport android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;


public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Toast.makeText(this, "哈哈,我成功启动了!", Toast.LENGTH_LONG).show();
        Log.e("AutoRun","哈哈,我成功启动了!");

	}

	
}

4.MyService

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.os.IBinder;

public class MyService extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);
		
		Intent i = new Intent(Intent.ACTION_MAIN);    
		i.addCategory(Intent.CATEGORY_LAUNCHER);                
		ComponentName cn = new ComponentName(packageName, className);                
		i.setComponent(cn);    
		startActivity(i);    
	}

}


目录
相关文章
|
21天前
|
Java API 调度
Android系统 自定义开机广播,禁止后台服务,运行手动安装应用接收开机广播
Android系统 自定义开机广播,禁止后台服务,运行手动安装应用接收开机广播
43 0
|
21天前
|
安全 Shell Android开发
Android系统 init.rc开机执行shell脚本
Android系统 init.rc开机执行shell脚本
51 0
|
22天前
|
Android开发
Android 11 添加Service服务SELinux问题
Android 11 添加Service服务SELinux问题
43 1
|
4天前
|
XML Java Android开发
利用Bundle实现Android Activity间消息的传递
利用Bundle实现Android Activity间消息的传递
11 2
|
22天前
|
Android开发
Android Service Call /dev/xxx SELinux
Android Service Call /dev/xxx SELinux
17 1
|
4天前
|
Android开发 数据库管理
Android如何在Activity和Service之间传递数据
Android如何在Activity和Service之间传递数据
10 3
|
7天前
|
Linux Android开发
Android开机不显示bootloader界面
Android开机不显示bootloader界面
9 0
Android开机不显示bootloader界面
|
7天前
|
Shell Android开发
Android Activity重写dump方法实现通过adb调试代码
Android Activity重写dump方法实现通过adb调试代码
13 0
|
8天前
|
Java Android开发
Android 开机动画的启动
Android 开机动画的启动
13 0
|
8天前
|
Android开发
Android Service的两种使用方法
Android Service的两种使用方法
15 2