[英]SMS Logger: how to create AndroidManifest.xml for app based on ContentObserver
I'am new to android apps... 我是Android应用程序的新手...
I wish to create as easiest as possible android background (no-gui) app based on ContentObserver that will monitor any sms in/out activities and notify me about this fact. 我希望基于ContentObserver创建尽可能简单的android后台(无GUI)应用程序,该应用程序将监视任何短信输入/输出活动,并通知我这一事实。
I've got this: 我有这个:
import android.content.Context;
import android.database.ContentObserver;
import android.os.Handler;
import android.util.Log;
public class SMSNotifyActivity extends ContentObserver {
private static String TAG ="SMSContentObserver";
private Context MContext ;
private Handler MHandler;
public SMSNotifyActivity(Context Context, Handler Handler) {
super(Handler);
MContext = Context;
MHandler = Handler;
}
@Override
public void onChange(boolean selfChange) {
Log.i(TAG, "The Sms Table Has Changed ") ;
}
}
I know that for ContentObserver I need to create a service in AndroidManifest.xml: 我知道对于ContentObserver,我需要在AndroidManifest.xml中创建一个服务:
<service android:name=".SMSNotifyActivity" />
and grant rights to app boot at start: 并在启动时授予启动应用程序的权限:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
...
<receiver android:name=".SMSNotifyActivity" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
But still sth is wrong... 但是……仍然是错误的……
I'am getting: Unable to instantiate activity ComponentInfo{com.example.smsnotify/com.example.smsnotify.SMSNotifyActivity}: java.lang.InstantiationException: com.example.smsnotify.SMSNotifyActivity 我得到:无法实例化活动ComponentInfo {com.example.smsnotify / com.example.smsnotify.SMSNotifyActivity}:java.lang.InstantiationException:com.example.smsnotify.SMSNotifyActivity
My whole AndroidManifest.xml: 我整个AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.smsnotify"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".SMSNotifyActivity"
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=".SMSNotifyActivity" />
<receiver android:name=".SMSNotifyActivity" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
You have a bit of confusion with Service
, BroadcastReceiver
, ContentObserver
and Activity
. 您对
Service
, BroadcastReceiver
, ContentObserver
和Activity
感到有些困惑。
Create a Service
(extend from Service
or IntentService
) and put your ContentObserver
in it as an inner class. 创建一个
Service
(从Service
或IntentService
扩展),然后将ContentObserver
作为内部类放入ContentObserver
。
Remove the <activity>
tag from your manifest, you don't need that. 从清单中删除
<activity>
标记,您不需要。
The <receiver>
element in your manifest should point to a BroadcastReceiver
extending class. 清单中的
<receiver>
元素应指向BroadcastReceiver
扩展类。 Try extending a BroadcastReceiver
, call it SMSNotifyStarter, put this in your manifest: 尝试扩展
BroadcastReceiver
,将其称为SMSNotifyStarter,将其放入清单中:
<receiver android:name=".SMSNotifyStarter" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
And when you get to broadcast of a boot, start your Service
. 当您广播引导时,请启动
Service
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.