记录下用广播方式开机启动service或activity,当然还有一种在init.rc中注册服务,待下次研究O( _ )O。
当Android系统完成BOOT阶段之后,就会发送一条名为 ACTION_BOOT_COMPLETED 的广播,我们便可在一个BroadcastReceiver中捕获这条广播,然后启动我们的Activity或者Service,当然要注意的是,我们的 application必须具有捕获该广播的权限:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
1.Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.gsm"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.RESTART_PACKAGES"/>
<uses-permission android:name="android.permission.GET_TASKS"/>
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
<application android:theme="@style/AppTheme">
<service
android:name=".GSMService"
android:process=":remote" >
<intent-filter>
<action android:name="com.app.gsm.GSMService"></action>
</intent-filter>
</service>
<receiver android:name="com.app.gsm.GSMServiceBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
</application>
</manifest>
2.GSMServiceBootReceiver.java
package com.app.gsm;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class GSMServiceBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent();
myIntent.setAction("com.app.gsm.GSMService");
context.startService(myIntent);
}
}
3.GSMService.java
/**
* GSMService.java
* TODO
*/
package com.app.gsm;
import java.lang.ref.WeakReference;
import android.app.ActivityManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.IBinder.DeathRecipient;
import android.os.Message;
import android.util.Log;
import android.widget.RemoteViews;
public class GSMService extends Service {
private static final String TAG = "GSMService";
public static GSMHandler mHandler;
//声明通知(消息)管理器
static {
try {
System.loadLibrary("ril-jni");
} catch (java.lang.Error e) {
e.printStackTrace();
}
}
public GSMService(){
if (!native_init(new WeakReference<GSMService>(GSMService.this))){
Log.d(TAG,"gsm throw runtimeException....");
throw new RuntimeException();
}
}
@Override
public void onCreate() {
super.onCreate();
mHandler = new GSMHandler(this);
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
public void dispatchDialog(int what) {
}
static class GSMHandler extends Handler implements DeathRecipient{
WeakReference<GSMService> reference;
GSMHandler(GSMService instance) {
reference = new WeakReference<GSMService>(instance);
}
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
GSMService me = reference.get();
if (me == null)
return;
Log.d(TAG,"handleMessage msg.what="+msg.what);
me.dispatchDialog(msg.what);
}
@Override
public void binderDied() {
// TODO Auto-generated method stub
}
}
private int sendMessage(int msg, int p1, int p2){
try {
Message m = mHandler.obtainMessage(msg);
m.arg1 = p1;
m.arg2 = p2;
m.obj = null;
m.sendToTarget();
Log.d(TAG,"sendMessage msg="+msg);
return 0;
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
@SuppressWarnings("unchecked")
static int native_proc(Object o, int msg, int p1, int p2,String incoming_num) {
WeakReference<GSMService> wo;
GSMService gsm;
incoming_call_num=incoming_num;
if (o == null)
return -1;
try {
wo = (WeakReference<GSMService>) o;
gsm = wo.get();
if (gsm == null)
return -1;
Log.d(TAG,"gsm native_proc,msg="+msg+"incoming_num="+incoming_num);
return gsm.sendMessage(msg, 0, 0);
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
private native boolean native_init(WeakReference<GSMService> wo);
}
这里已基本实现开机启动service,并且使用jni从c层传递消息,到java处理。