您的位置:首页 > 移动开发 > Android开发

Android系统通知监听方案

2017-09-10 17:29 225 查看

1.继承NotificationListenerService类,复写两个方法

public class NLService extends NotificationListenerService {
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
}

@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
}
}


2.在AndroidManifest.xml中注册Service并声明相关权限;

 
<service android:name=".NLService"
android:label="@string/service_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>

3.配置系统级权限

1 、通过shareduserid来获取系统权限

                (1) 在AndroidManifest.xml中添加android:sharedUserId="android.uid.system"

                (2) 在Android.mk文件里面添加LOCAL_CERTIFICATE := platform(使用系统签名)

                (3) 在源码下面进行mm编译,这样生成的apk能够获取system权限,可以在任意system权限目录下面进行目录或者文件的创建,以及访问其他apk

2、使用手机的platform平台签名后,能够获取到系统权限。
1>在AndroidManifest.xml设置android:sharedUserId="android.uid.system"。

2>编译通过后,从eclipse导出未签名的SetGPS.apk。

3>使用\out\host\Linux-x86\framework\signapk.jar +\build\target\product\security\platform.pk8 +platform.x509.pem
      把这三个文件放到同一个文件中

4>执行“Java -jar signap
4000
k.jar platform.x509.pem platform.pk8 SetGPS.apk SetGPSSigned.apk”做平台签名得到SetGPSSigned.apk。

    这样不用root push到system/app/ ,直接 install 安装后即可使用。

5>编译通过后,从eclipse导出未签名的SetGPS.apk。
4.代码增加授权

Settings.Secure.putString(this.getContentResolver(), "enabled_notification_listeners",
new ComponentName(getApplicationContext(), com.example.notification.NotificationMonitorService.class).flattenToString());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐