0

アンドロイドアプリ初心者です...

SMSのイン/アウトアクティビティを監視し、この事実について通知するContentObserverに基づいて、できるだけ簡単なAndroidバックグラウンド(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>

しかし、それでも sth は間違っています...

私は取得しています: アクティビティ 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>
4

1 に答える 1

1

ServiceBroadcastReceiverContentObserverおよび と少し混乱していActivityます。

Servicea (extend from Serviceor ) を作成し、その中に yourを内部クラスとしてIntentService入れます。ContentObserver

マニフェストからタグを削除し<activity>ます。それは必要ありません。

マニフェスト内の要素は、拡張クラス<receiver>を指している必要があります。BroadcastReceivera を拡張してみてBroadcastReceiver、それを SMSNotifyStarter と呼び、これをマニフェストに入れます。

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

そして、ブートのブロードキャストに到達したら、Service.

于 2012-04-19T08:22:22.433 に答える