SMS Logger:如何为基于ContentObserver的应用程序创建AndroidManifest.xml



我是安卓应用程序的新手...

我希望创建基于ContentObserver的尽可能简单的Android后台(no-gui)应用程序,该应用程序将监控任何短信进出活动并通知我这一事实。

我有这个:

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>

但还是错了...

I'am getting: T無法實化活動 ComponentInfo{com.example.smsnotify/com.example.smsnotify.SMSNotifyActivity}: java.lang.InstantiationException: com.example.smsnotify.SMSNotifyActivity

我的整个安卓清单.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>

你对ServiceBroadcastReceiverContentObserverActivity有点混淆。

创建一个Service(从ServiceIntentService扩展),并将ContentObserver作为内部类放入其中。

从清单中删除 <activity> 标记,您不需要它。

清单中的 <receiver> 元素应指向BroadcastReceiver扩展类。尝试扩展BroadcastReceiver,称之为SMSNotifyStarter,将其放在清单中:

<receiver android:name=".SMSNotifyStarter" >
   <intent-filter>
      <action android:name="android.intent.action.BOOT_COMPLETED" />
   </intent-filter>
</receiver>

当你开始广播启动时,开始你的Service.

最新更新