2016-04-07 5 views
1

ここにレシーバファイルがあります。私が感じることは、どんな種類の問題もありません。私はちょうど渡された意図のタイプを印刷します。私によれば、onReceiveは決して呼ばれません。私はマニフェスト自体に問題があると思いますか?または他のアイデア?レシーバは、アプリケーション内の他のコードにリンクされていません。Screen On/Off BroadcastReceiverが機能しないのはなぜですか?

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> 

私は受信者を動的に追加/登録/登録解除していません。私はここでいくつかの助けを必要とするでしょう。

+1

両方以下登録済みのレシーバ。静的に登録されたReceiverクラスはそれらを取得しません。 –

+0

あなたのパッケージ名は "sharukh.locky"でしょうか?同じパッケージ内のあなたの放送受信者ですか? –

答えて

1

あなたは次のように放送の動的な呼び出しを試みることによってこれを行うことができますが、彼らは動的にのみ受信することができることを意味しFLAG_RECEIVER_REGISTERED_ONLY`、 `で放送されているため、あなたのレシーバを登録したアクションの

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)); 
    } 
} 
+1

完璧に動作し、私は 'FLAG_RECEIVER_REGISTERED_ONLY'のコンセプトを知らなかった。ありがとう。 –

+0

実際に[ACTION_SCREEN_OFFのドキュメント](http://developer.android.com/reference/android/content/Intent.html#ACTION_SCREEN_OFF)に記載されていることに注意してください。現れます。 – SpaceBison

関連する問題