簡體   English   中英

為什么我的 Screen On/Off BroadcastReceiver 不工作?

[英]Why isn't my Screen On/Off BroadcastReceiver working?

這是我的接收器文件。 我覺得沒有任何問題。 我只是打印傳遞的意圖類型。 據我說 onReceive 從來沒有被調用過。 我猜我的清單本身有問題? 還是有其他想法? 接收器未鏈接到應用程序中的任何其他代碼。

package sharukh.locky;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class BroadCastReceiver extends BroadcastReceiver
{
    public BroadCastReceiver()
    {
    }

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.i("rece", intent.getAction());
    }
}

我的清單看起來也不錯,但我不知道為什么它不起作用。

<?xml version="1.0" encoding="utf-8"?>
<manifest package="sharukh.locky"
          xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="AIzaSyAGufoN9huWkmw8nTskk2aaFHW1gXn1Z7Q"/>
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/>

        <!-- Activities -->
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name=".Tutorial"/>
        <activity android:name=".TutorialSelectApp"/>
        <activity
            android:name=".LockScreen"
            android:label="@string/title_activity_lock_screen"
            android:theme="@style/AppTheme.NoActionBar"/>

        <!-- Services -->
        <service
            android:name=".LockyService"
            android:enabled="true"
            android:exported="true"/>

        <receiver
            android:name=".BroadCastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.SCREEN_OFF"/>
                <action android:name="android.intent.action.HEADSET_PLUG"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

我沒有動態添加/注冊/取消注冊任何接收器。 我需要一些幫助。

您可以通過嘗試動態調用廣播來做到這一點,如下所示

public class YourActivity extends Activity {

    //Create broadcast object
    BroadcastReceiver mBroadcast = new BroadcastReceiver() {    
    //When Event is published, onReceive method is called
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Log.i("[BroadcastReceiver]", "MyReceiver");

        if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            Log.d("[BroadcastReceiver]", "Screen ON");
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            Log.d("[BroadcastReceiver]", "Screen OFF");
        }

    }
};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        registerReceiver(mBroadcast, new IntentFilter(Intent.ACTION_SCREEN_ON));
        registerReceiver(mBroadcast, new IntentFilter(Intent.ACTION_SCREEN_OFF));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM