service强制运行在phone进程

<service android:name=".atservice.ATService"  android:process="com.android.phone"> 
    <intent-filter>
        <action android:name="com.sagereal.atcmd.service.ACTION"/>
    </intent-filter>
</service>

以上是四大组件之一服务的注册。若不指定process,则进程名为包名;若要指定运行在某一进程中,如phone进程,则组件要在注册时加上:android:process="com.android.phone"。若要运行在phone进程,此时组件所在模块的Android.mk文件要有:"LOCAL_CERTIFICATE := platform"方可,这句表示使用platform来签名,即系统级别的签名。

注:

1). 获取当前进程名

    /**
     * 获取当前进程的名字,一般就是当前app的包名
     *
     * @param context 当前上下文
     * @return 返回进程的名字
     */
    public static String getAppName(Context context){
        int pid = android.os.Process.myPid(); // Returns the identifier of this process
        ActivityManager am = 
                (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
        if (runningApps == null) {
            return null;
        }
        for (ActivityManager.RunningAppProcessInfo processInfo : runningApps) {
            if (processInfo.pid == pid) {// 根据进程的信息获取当前进程的名字
                return processInfo.processName;
            }
        }
        return null;
    }

2). action是在intent-filter内的,若书写不对,会报错:INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES。

3). 清单文件修改之后,要重启手机,才有效果。

参考:

Android——编译release版签名系统

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值