温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android应用实现安装后自启动的方法

发布时间:2020-08-26 18:35:21 来源:脚本之家 阅读:343 作者:俩兜没钱 栏目:移动开发

和网上大多数方法一样,使用广播手段:

ACTION_PACKAGE_ADDED 一个新应用包已经安装在设备上,数据包括包名(最新安装的包程序不能接收到这个广播)

ACTION_PACKAGE_REPLACED 一个新版本的应用安装到设备,替换之前已经存在的版本

ACTION_PACKAGE_CHANGED 一个已存在的应用程序包已经改变,包括包名

ACTION_PACKAGE_REMOVED 一个已存在的应用程序包已经从设备上移除,包括包名(正在被安装的包程序不能接收到这个广播)

ACTION_PACKAGE_RESTARTED 用户重新开始一个包,包的所有进程将被杀死,所有与其联系的运行时间状态应该被移除,包括包名(重新开始包程序不能接收到这个广播)

ACTION_PACKAGE_DATA_CLEARED 用户已经清除一个包的数据,包括包名(清除包程序不能接收到这个广播)

直接思路:注册广播接收以上需要的action来实现。

但是,在安卓3.1之后,有了以下机制:

force-stop in Manage Application of Settings makes App in a stopped state!

Here is what Google describes

What is Stopped State

Starting from Android 3.1, the system's package manager keeps track of applications that are in a stopped state and provides a means of controlling their launch from background processes and other applications.

Note that an application's stopped state is not the same as an Activity's stopped state. The system manages those two stopped states separately.

Why Android Adds this

Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. It does this to prevent broadcasts from background services from inadvertently or unnecessarily launching components of stoppped applications. A background service or application can override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES flag to broadcast intents that should be allowed to activate stopped applications.

As the above references point out it will prevent broadcast intents delivering to stopped packages. Actually this control mechanism will ensure safety and save energy.

Android 3.1 APIs

翻译:

在 系统设置 - 应用管理 中的“强制停止” 作用是让app处于(stopped)停止状态。

下面是google的官方描述:

什么是停止状态?

从Andriod3.1开始,系统包管理服务会一直追踪处于停滞状态的app,并提供了控制它们从后台进程或其他应用程序启动的方法。

注意:应用程序的停止状态不同于activity(活动)的停止状态。系统是分开来处理这两类停止状态的。

为什么Android要添加这个功能?

注意:系统为所有用于发送广播的Intent默认添加了FLAG_EXCLUDE_STOPPED标志。这样做是为了阻止发送自后台service的广播不小心启动某个已停止应用的组件。一个后台service服务或app应用程序可以

通过向广播的Intent对象添加FLAG_INCLUDE_STOPPED_PACKAGES标志,覆盖重写这个行为,使得该广播可以激活处于停止状态的应用程序。

上述描述指出:系统默认会阻止停止状态的app接收广播。这个控制机制的目的是保证安全、节约电量。

所以,要实现安装apk后自启动,前提是

1、触发ACTION_PACKAGE_REPLACED 广播(也就是apk覆盖替换安装才接收的到,初次安装的广播ACTION_PACKAGE_ADDED 不会被当前安装包触发,因为该app未运行过)

2、在app项目中使用静态注册广播(因为动态广播是app运行后才可以接受到)

3、app曾经运行过(即不处于stopped状态)

在Android5.1真机上测试:

初次安装的app不会触发广播。

覆盖安装未运行过的app,不会触发广播

安装完运行app后,退出App(点击返回键、并从recent任务中移除,此时在设置-应用中查看,app仍未处于stop状态)。覆盖安装后,app成功自动运行。(可看做实现安装后自启动)

此时退出App,并在设置-应用中把app进行【强制停止】。覆盖安装后,app没有自动运行。(此时在设置-应用中查看,app处于stop状态)

所以,只要在App运行时,直接覆盖安装apk,是可以用广播接收器实现安装完后自启动的。 (至少在android 5.1上 ^,^)

下面简单介绍下代码:

(1)自定义广播接收器:

public class MyReceiver extends BroadcastReceiver {
 
	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		String localPkgName = context.getPackageName();//取得MyReceiver所在的App的包名
		Uri data = intent.getData();
		String installedPkgName = data.getSchemeSpecificPart();//取得安装的Apk的包名,只在该app覆盖安装后自启动
		if((action.equals(Intent.ACTION_PACKAGE_ADDED)
				|| action.equals(Intent.ACTION_PACKAGE_REPLACED)) && installedPkgName.equals(localPkgName)){
			Intent launchIntent = new Intent(context,MainActivity.class);
			launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(launchIntent);
		}
	}
}

(2)AndroidManifest.xml中静态注册广播接收器

<application
  android:allowBackup="true"
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name"
  android:theme="@style/AppTheme" >
  <activity
   android:name="com.example.mydemo.MainActivity"
  	android:configChanges="orientation|keyboard"
   android:label="@string/app_name">
   <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
  </activity>
  <receiver android:name="com.example.mydemo.MyReceiver">
   <intent-filter>
    <action android:name="android.intent.action.PACKAGE_ADDED" /> 
    <action android:name="android.intent.action.PACKAGE_REPLACED" />
    <action android:name="android.intent.action.PACKAGE_REMOVED" />
    <data android:scheme="package"/>
   </intent-filter>
  </receiver>
 </application>

以上这篇Android应用实现安装后自启动的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI