0

こんにちは、BroadCastReciver に取り組んでいます。BroadCastReciver を定義するには 2 つの方法があります。1 つ目は Java コードを使用する方法で、2 つ目は を使用して AndroidManifest.xml で定義する方法です。私のコードでは、2番目のコードは正しく動作しません。どこが間違っているのか教えてください。

public class HotelReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        String dActionName = intent.getAction();
        Log.i("My Rceiver ", intent.getAction());
        if (dActionName.equals(Intent.ACTION_SCREEN_ON)) {
            Toast.makeText(context, "SCREEN ON", Toast.LENGTH_SHORT).show();
        } else if (dActionName.equals(Intent.ACTION_SCREEN_OFF)) {

        }
    }

} 

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hotelsecurity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="10"
        android:maxSdkVersion="15" />
<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver 
            android:name=".HotelReceiver">
            <intent-filter>
                <action android:name="android.intent.action.SCREEN_ON"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>
4

2 に答える 2

0

受信者は読むべきだと思います

    <receiver 
        android:name="HotelReceiver">
        <intent-filter>
            <action android:name="android.intent.action.SCREEN_ON"/>
        </intent-filter>
    </receiver>

ドット「.」なし

于 2013-03-27T07:19:43.827 に答える
0

アクティビティの onCreate() 内でこのコードを使用するだけで、

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    HotelReceiver mReceiver = new HotelReceiver(this);
    registerReceiver(mReceiver, filter);
于 2013-03-27T07:59:16.870 に答える