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

所属分类: 软件编程 / Android 阅读数: 47
收藏 0 赞 0 分享

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

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应用实现安装后自启动的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享

TextView显示系统时间(时钟功能带秒针变化

用System.currentTimeMillis()可以获取系统当前的时间,我们可以开启一个线程,然后通过handler发消息,来实时的更新TextView上显示的系统时间,可以做一个时钟的功能
收藏 0 赞 0 分享

Android用ListView显示SDCard文件列表的小例子

本文简单实现了用ListView显示SDCard文件列表,目录的回退等功能暂不讨论,获取文件列表,files即为所选择目录下的所有文件列表
收藏 0 赞 0 分享

Android拦截外拨电话程序示例

这篇文章主要介绍了Android拦截外拨电话的示例,大家参考使用吧
收藏 0 赞 0 分享

通过Html网页调用本地安卓(android)app程序代码

如何使用html网页和本地app进行传递数据呢?经过研究,发现还是有方法的,总结了一下,大致有一下几种方式
收藏 0 赞 0 分享

android Textview文字监控(Textview使用方法)

以手机号充值为例,当用户输入最后一位数时候,进行汇率的变换,本文就实现类似这样的功能
收藏 0 赞 0 分享

Android ListView长按弹出菜单二种实现方式示例

这篇文章主要介绍了Android ListView长按弹出菜单的方法,大家参考实现
收藏 0 赞 0 分享
查看更多