android activity/service开机后自动运行
时间:2010-05-31 来源:fandaydai
android activity/service开机后自动运行 (转) 收藏
http://blog.chinaunix.net/u/20947/showart_1945057.html
看了网上的几个 例子,也做了一个系统启动后直接运行activity的小程序
代码贴在下面:
首先是从BroadcastReceiver派生出一个新类,用来监听系统启动后发出的广播消息android.intent.action.BOOT_COMPLETED。
BootReceiver.java:
import android.content.BroadcastReceiver;
//if you want to start a service, follow below method: /*******************************************************
Intent service = new Intent(yourService.ACTION_START);
我改成:
Intent i = new Intent(AutoRun.class.getName()); context.startService(i);
******************************************************/ |
接下来这个类就是监听到系统启动完毕后,我们要运行的activity.
FirstRun.java
import android.app.Activity; |
当然,我们还要改配置文件,需要注意的是,在manifest.xml中需要加上
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.service.prac"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name=".BootReceiver"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
<activity android:name=".FirstRun">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
</manifest>