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 activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List list = activityManager.getRunningAppProcesses();
        Iterator i = list.iterator();
        while (i.hasNext())
        {
            ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i.next());
            try
            {
                if (info.pid == pid)
                {
                    // 根据进程的信息获取当前进程的名字
                    return info.processName;
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        // 没有匹配的项,返回为null
        return null;
    }

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

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

参考:

Android——编译release版签名系统

猜你喜欢

转载自blog.csdn.net/lyl0530/article/details/81174496