android开机启动service

面试:你懂什么是分布式系统吗?Redis分布式锁都不会?>>>   hot3.png

记录下用广播方式开机启动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>

GSMServiceBootReceiver.java


package com.app.gsm;


import android.content.BroadcastReceiver;


import android.content.Context;


import android.content.Intent;


public class GSMServiceBootReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {


Intent myIntent = new Intent();

myIntent.setAction("com.app.gsm.GSMService");


context.startService(myIntent);


}


}

GSMService.java


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();


}


}


public void onCreate() {


super.onCreate();

mHandler = new GSMHandler(this);


}


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);


}


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);


}

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处理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值