博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android开机启动service
阅读量:5886 次
发布时间:2019-06-19

本文共 4017 字,大约阅读时间需要 13 分钟。

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

转载于:https://my.oschina.net/gavinjin/blog/209367

你可能感兴趣的文章
游戏开发基础:方向键的组合,八方向实现
查看>>
黑书-DP-方块消除 ****
查看>>
MySQL 分区
查看>>
我的架构经验系列文章 - 后端架构 - 语言层面
查看>>
DEFERRED_SEGMENT_CREATION
查看>>
读取手机硬件信息
查看>>
一致哈希
查看>>
The connection to adb is down, and a severe error has occured. 问题解决
查看>>
在Jenkins中配置运行远程shell命令
查看>>
代码杂记
查看>>
linux中防CC攻击两种实现方法(转)
查看>>
《Programming WPF》翻译 第9章 4.模板
查看>>
Windows7+VS2012下OpenGL 4的环境配置
查看>>
Linux Kernel中断子系统来龙去脉浅析【转】
查看>>
Linux NFS服务器的安装与配置
查看>>
Ada boost学习
查看>>
Unity中SendMessage和Delegate效率比较
查看>>
Linux下EPoll通信模型简析
查看>>
react-native 制作购物车ShopCart
查看>>
Linux服务器 java生成的图片验证码乱码问题
查看>>