我是android应用程序的新手...
我希望基于 ContentObserver 创建尽可能简单的 android 背景(无 gui)应用程序,该应用程序将监视任何 sms 输入/输出活动并通知我这一事实。
我有这个:
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 ") ;
}
}
我知道对于 ContentObserver 我需要在 AndroidManifest.xml 中创建一个服务:
<service android:name=".SMSNotifyActivity" />
并在启动时授予应用启动权限:
<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>
但还是……错了……
我得到:无法实例化活动 ComponentInfo{com.example.smsnotify/com.example.smsnotify.SMSNotifyActivity}:java.lang.InstantiationException:com.example.smsnotify.SMSNotifyActivity
我的整个 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>