
【原理】
当Android系统完成BOOT阶段之后,就会发送一条名为 ACTION_BOOT_COMPLETED 的广播,我们便可在一个BroadcastReceiver中捕获这条广播,然后启动我们的Activity或者Service,当然要注意的是,我们的application必须具有捕获该广播的权限,下面请看具体步骤:
【步骤一】首先要有一个用于开机启动的Activity或者Service,这里以系统自己创建的最简单的Activity为例进行讲解。
package com.billhoo.study
import android.app.Activity
import android.os.Bundle
public class BootTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
}
}
【步骤二】我们要编写一个BroadcastReceiver用以捕获ACTION_BOOT_COMPLETED这条广播,并在捕获之后启动我们要启动的Activity。
注意:必须在intent中添加Intent.FLAG_ACTIVITY_NEW_TASK标记,这就是我之前老是启动失败的原因。至于为什么,我还在研究SDK doc,明白了之后就回来补上。
package com.billhoo.study
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
public class BootCompletedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
{
Intent newIntent = new Intent(context, BootTestActivity.class)
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) //注意,必须添加这个标记,否则启动会失败
context.startActivity(newIntent)
}
}
}
【步骤三】在AndroidManifest.xml配置文件中注册我们的BroadcastReceiver
<receiver android:name=".BootCompletedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
【步骤四】在AndroidManifest.xml配置文件中添加允许我们捕获该广播的权限
<!-- permissions -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
之前把uses-permission 打错成permission ,结果一直提示下面这个错误:Permission Denial: receiving Intent { act=android.intent.action.BOOT_COMPLETED flg=0x8000010 (has extras) } to com.example.boottest/.BootCompletedReceiver requires android.permission.RECEIVE_BOOT_COMPLETED due to sender null (uid 1000)
重启虚拟机,大功告成。
下面附上完成的AndroidManifest.xml,以便大家理解参考
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.billhoo.study" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<!-- permissions -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<!-- activities -->
<activity android:name=".BootTestActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- receivers -->
<receiver android:name=".BootCompletedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
命令行打开方式:1、首先你要打开android模拟器
(下面命令行打开的4步骤我是引用百度上的)
1).找到SDK的tools文件夹,我的在D:\android-sdk-windows\tools; 2).如果没有创建AVD的话,可以用命令android list targets查看各版本对应的id; 然后android create avd --target 5 --name Android2.2//我这里5对应的是android2.2 3).用命令android list avd查看自己以创建的AVD 4).emulator -debug avd_config -avd Android2.2就可以打开AVD了,就是有点慢
或者在eclipse上直接打开一个android程序。
2、然后输入 adb install xxx.apk
,在模拟器上点击对应应用即可(安装apk后的应用程序名不知道的话得仔细找哦,肯定在模拟器上的)。
注:xxx.apk包含路径名,在命令行你只要直接把apk文件拖至windows命令窗口就可以加载完整路径了。
代码打开方式:
需要将apk拷贝至shared_prefs文件夹下
(Eclipse下工具栏window--show view--other--Android--File Explorer,
也许打开的File Explorer为空白,这个你肯定忘了先运行下面代码构成的工程。出现目录后找到data/data/工程包名/shared_prefs,ok)
public class APKTest extends Activity { private SharedPreferences metafer = nullApplicationInfo mAppInfo = null public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState) setContentView(R.layout.main) //apk安装或卸载路径 String installPath = "/data/data/com.hyz/shared_prefs/matchmusic.apk" //新建shared_prefs文件夹 mkShared_prefs() //安装apk installApk(installPath) //卸载apk dumpApk(installPath) } public void dumpApk(String path) { ApplicationInfo mAppInfo = null PackageManager pm = getApplicationContext().getPackageManager() PackageInfo info = pm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES) if(info != null) { mAppInfo = info.applicationInfo } Uri uri = Uri.fromParts("package", mAppInfo.packageName, null) Intent it = new Intent(Intent.ACTION_DELETE, uri) startActivity(it) } public void installApk(String path) { Intent ret = new Intent() ret.setDataAndType(Uri.fromFile(new File(path)),"application/vnd.android.package-archive") ret.setAction(Intent.ACTION_VIEW) startActivity(ret) } public void mkShared_prefs() { if (metafer == null) { // metafer = getSharedPreferences("Vdmc", 0) metafer = PreferenceManager.getDefaultSharedPreferences(this) } SharedPreferences.Editor editor = metafer.edit() //editor.putString("IMSI", "") editor.commit() } }
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)