Android 在注册ContentObserver之前,如何侦听要安装的包?
我这里的问题更多的是我使用的框架的结果,而不是Android本身。在用户安装了与AWARE framework关联的特定插件后,我的应用程序需要注册ContentObserver。该框架有一个用于安装插件的传感器,并在发生这种情况时发送广播。文档可在此处找到: 我想使用广播接收器来完成这项工作 关于如何正确地做到这一点,我有几个问题:Android 在注册ContentObserver之前,如何侦听要安装的包?,android,android-intent,broadcastreceiver,android-manifest,android-broadcast,Android,Android Intent,Broadcastreceiver,Android Manifest,Android Broadcast,我这里的问题更多的是我使用的框架的结果,而不是Android本身。在用户安装了与AWARE framework关联的特定插件后,我的应用程序需要注册ContentObserver。该框架有一个用于安装插件的传感器,并在发生这种情况时发送广播。文档可在此处找到: 我想使用广播接收器来完成这项工作 关于如何正确地做到这一点,我有几个问题: 我对本文档和Android的理解是,如果我在清单文件中使用意图过滤器“Installations.EXTRA_PACKAGE_NAME”注册一个Broadcast
package hcii.tracker;
public class MainService extends Service {
public static final Uri ACTIVITY_URI = Uri.parse("content://com.aware.plugin.google.activity_recognition.provider/plugin_google_activity_recognition");
public HashMap<Uri,ContentObserver> mContentObservers;
public void onCreate(){
Log.d("SERVICE", "Service created!");
Context context = this;
mContentObservers = new HashMap<Uri,ContentObserver>();
//Activate installations sensor
Aware.setSetting(context, Aware_Preferences.STATUS_INSTALLATIONS, true);
//Activate Accelerometer
Aware.setSetting(this, Aware_Preferences.STATUS_ACCELEROMETER, true);
//Set sampling frequency
Aware.setSetting(this, Aware_Preferences.FREQUENCY_ACCELEROMETER, 60);
Aware.setSetting(getApplicationContext(), "frequency_google_fused_location", 60,
"com.aware.plugin.google.fused_location");
Aware.startPlugin(getApplicationContext(), "com.aware.plugin.google.fused_location");
Aware.startPlugin(getApplicationContext(), "com.aware.plugin.google.activity_recognition");
sendBroadcast(new Intent(Aware.ACTION_AWARE_REFRESH));
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onDestroy() {
try {
ContentObserver observer = mContentObservers.get(ACTIVITY_URI);
getContentResolver().unregisterContentObserver(observer);
mContentObservers.remove(ACTIVITY_URI);
} catch (IllegalStateException ise) {
Log.d("SERVICE", "No ContentObservers registered");
}
}
public class ActivityRecognitionObserver extends ContentObserver {
public Uri CONTENT_URI = Uri.parse("content://com.aware.plugin.google.activity_recognition.provider/plugin_google_activity_recognition");
public ActivityRecognitionObserver(Handler handler) {
super(handler);
}
public void onChange(boolean selfChange) {
super.onChange(selfChange);
// Get the latest recorded value
Log.d("OBSERVER", "Change in activity data detected");
Cursor activity = getContentResolver().query(CONTENT_URI, null, null, null,
"activity_name" + "DESC LIMIT 1");
if( activity != null && activity.moveToFirst() ) {
// Here we read the value
String activity_name = activity.getString(activity.getColumnIndex("activity_name"));
if (activity_name.equals("in_vehicle")){
Aware.setSetting(getApplicationContext(), "frequency_google_fused_location", 60,
"com.aware.plugin.google.activity_recognition");
Log.d("OBSERVER", "Recognized in vehicle");
Aware.startPlugin(getApplicationContext(), "com.aware.plugin.google.activity_recognition");
}
else {
Aware.setSetting(getApplicationContext(), "frequency_google_fused_location", 180,
"com.aware.plugin.google.activity_recognition");
Log.d("OBSERVER", "Recognized on foot");
Aware.startPlugin(getApplicationContext(), "com.aware.plugin.google.activity_recognition");
}
}
if( activity != null && ! activity.isClosed() ) activity.close();
}
}
public class InstallationReceiver extends BroadcastReceiver {
public InstallationReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null && action.equals("com.aware.plugin.google.activity_recognition")){
ActivityRecognitionObserver so = new ActivityRecognitionObserver(new Handler());
getContentResolver().registerContentObserver(ACTIVITY_URI, true, so);
mContentObservers.put(ACTIVITY_URI, so);
Log.d("SERVICE", "Observer registered");
}
}
}
}
package hcii.tracker;
公共类MainService扩展服务{
公共静态最终Uri活动\u Uri=Uri.parse(“content://com.aware.plugin.google.activity_recognition.provider/plugin_google_activity_recognition");
公共HashMap mcontentobserver;
public void onCreate(){
Log.d(“服务”,“服务已创建!”);
上下文=这个;
mcontentobserver=newhashmap();
//启动传感器
Aware.setSetting(上下文,Aware_Preferences.STATUS_INSTALLATIONS,true);
//启动加速计
Aware.setSetting(这是Aware_Preferences.STATUS_Accelerator,true);
//设置采样频率
Aware.setSetting(这是Aware\u Preferences.FREQUENCY\u加速计,60);
Aware.setSetting(getApplicationContext(),“频率\u谷歌\u融合\u位置”,60,
“com.aware.plugin.google.fused_location”);
startPlugin(getApplicationContext(),“com.Aware.plugin.google.fused_location”);
startPlugin(getApplicationContext(),“com.Aware.plugin.google.activity_recognition”);
sendBroadcast(新意图(感知.动作感知\刷新));
}
@凌驾
公共IBinder onBind(意向){
返回null;
}
公共空间{
试一试{
ContentObserver=mcontentobserver.get(活动\ URI);
getContentResolver()。取消注册ContentObserver(observer);
mcontentobserver.remove(活动\ URI);
}捕获(非法状态){
Log.d(“服务”,“未注册内容”);
}
}
公共类ActivityRecognitionObserver扩展了ContentObserver{
公共Uri内容\u Uri=Uri.parse(“content://com.aware.plugin.google.activity_recognition.provider/plugin_google_activity_recognition");
公共活动RecognitionObserver(处理程序){
超级(处理器);
}
公共void onChange(布尔自更改){
super.onChange(selfChange);
//获取最新记录的值
Log.d(“观察者”,“检测到的活动数据变化”);
游标活动=getContentResolver().query(内容URI,null,null,null,
“活动名称”+“描述限制1”);
if(activity!=null&&activity.moveToFirst()){
//这里我们读取值
字符串activity_name=activity.getString(activity.getColumnIndex(“activity_name”));
if(活动名称等于(“车辆内”){
Aware.setSetting(getApplicationContext(),“频率\u谷歌\u融合\u位置”,60,
“com.aware.plugin.google.activity_recognition”);
日志d(“观察员”,“在车辆中识别”);
startPlugin(getApplicationContext(),“com.Aware.plugin.google.activity_recognition”);
}
否则{
Aware.setSetting(getApplicationContext(),“频率\谷歌\融合\位置”,180,
“com.aware.plugin.google.activity_recognition”);
Log.d(“观察员”,“步行确认”);
startPlugin(getApplicationContext(),“com.Aware.plugin.google.activity_recognition”);
}
}
如果(activity!=null&&!activity.isClosed())activity.close();
}
}
公共类InstallationReceiver扩展了BroadcastReceiver{
公共安装接收器(){
}
@凌驾
公共void onReceive(上下文、意图){
String action=intent.getAction();
if(action!=null&&action.equals(“com.aware.plugin.google.activity\u recognition”)){
ActivityRecognitionObserver so=新ActivityRecognitionObserver(新处理程序());
getContentResolver().registerContentObserver(活动URI,true,so);
put(ACTIVITY_URI,so);
日志d(“服务”、“观察员注册”);
}
}
}
}
Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="hcii.tracker" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
tools:replace="android:icon, android:theme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MainService"
android:exported="false" />
<receiver android:name="hcii.tracker.MainService$InstallationReceiver" >
<intent-filter>
<action android:name="Installations.EXTRA_PACKAGE_NAME" >
</action>
</intent-filter>
</receiver>
...
...
这里有几个问题:
1) 您可以为您的BroadcastReceiver
使用一个内部类,但如果是这样,则必须声明static
,因为Android需要能够使用new
实例化类本身。这在您当前的体系结构中是不可能的(因为该类是一个内部类)。一般来说,使用独立类更好(更容易)
2) 你的意图过滤器是错误的。Aw的链接文档
<intent-filter>
<action android:name="Installations.ACTION_AWARE_APPLICATION_ADDED"/>
</intent-filter>
public void onReceive(Context context, Intent intent) {
String packageName = intent.getStringExtra("Installations.EXTRA_PACKAGE_NAME");
if (packageName != null && packageName.equals("your.plugin.package.name") {
// Do your stuff here...
}
}