2013-04-23 24 views
7

BroadcastReceiverを使用しようとしていますが、動作していません。この問題を解決するのを手伝ってください。 MyReceiver.javaBroadcastReceiver for Screen On/Off not working

package com.example.broadcast_receiver; 

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

public class MyReceiver extends BroadcastReceiver { 

    @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.i("[BroadcastReceiver]", "Screen ON"); 
     } 
     else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){ 
      Log.i("[BroadcastReceiver]", "Screen OFF"); 
     } 
    } 
} 

のAndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.broadcast_receiver" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="9" 
     android:targetSdkVersion="16" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 

     <receiver android:name=".MyReceiver" 
      android:enabled="true" 
      android:exported="false"> 
      <intent-filter> 
       <action android:name="android.intent.action.SCREEN_ON"/> 
       <action android:name="android.intent.action.SCREEN_OFF"/> 
      </intent-filter> 
     </receiver> 

     <activity 
      android:name="com.example.broadcast_receiver.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

BroadcastReceiverは、すべてのログを作る作業をしていないではない、この問題を解決するために私を助けてください。

+0

を確認してください。http://stackoverflow.com/a/9478013/1777090 –

+0

他のサービスをレジスターに使用してください。メッセージ受信または着信呼。 LOgを確認してください。 –

+0

これはあなたのために完全に役立つかもしれないチェックしてくださいhttp://stackoverflow.com/questions/9477922/android-broadcast-receiver-for-screen-on-and-screen-off – nilkash

答えて

23

、私は...それは仕事を無愛想になる

public class MainActivity extends Activity { 

    //Create broadcast object 
    BroadcastReceiver mybroadcast = 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.i("[BroadcastReceiver]", "Screen ON"); 
      } 
      else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { 
       Log.i("[BroadcastReceiver]", "Screen OFF"); 
      } 

     } 
    }; 

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

     registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_ON)); 
     registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_OFF)); 
    } 
} 
+1

yaa働いています、bt私は自分のアプリを閉じた後に仕事をしたいと思いますbcoz私はロック画面を作っています。 – user2290872

+0

あなたはこのサービスを "サービス"の中で呼んでいます – kavya

+0

kk thnx私はそれをしましたが、アプリケーションを閉じていませんでしたnそれはうまくいきません。 、それであなたは4を助けることができますか? – user2290872

3

あなたが取得し、バッテリ値を試すことができます:あなたはこの受信機をしたい場合はちょっと放送の動的呼び出しを使用してみてください

public class Broadcast extends Activity { 
    //Create broadcast object 
    BroadcastReceiver mybroadcast = new BroadcastReceiver() { 

     //When Event is published, onReceive method is called 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      //Get battery percentage 
      int batterylevel = intent.getIntExtra("level", 0);  
      //get progressbar 
      ProgressBar myprogressbar = (ProgressBar) findViewById(R.id.progressbar); 
      myprogressbar.setProgress(batterylevel); 
      TextView tv = (TextView) findViewById(R.id.textfield); 
      //Set TextView with text 
      tv.setText("Battery Level: " + Integer.toString(batterylevel) + "%"); 
     } 
    }); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_broadcast); 
     registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 
    } 
} 
+0

それは働いている、私はそれが欲しいです私のアプリを閉じた後に仕事をするbcoz私はロック画面を作っているので、それについては何か考えている? – user2290872

+0

あなたはこのサービスを "サービス"の中で呼び出します – kavya

0

これを試してみましたシステムによって呼び出されると、それをエクスポートする必要があります。 exported = "false"を設定し、これをtrueに変更するか、エクスポートを完全に削除すると、作業が開始されます。通常、これは安全ではありませんが、SCREEN_ONとSCREEN_OFFの両方が保護されたブロードキャストであり、アクションを確認すると、より信頼できるシステムコードのみが送信されるため、かなり安全です。

この場合、インテントのブロードキャストには次のフラグが設定されているため、この場合は悲しいことです。 Intent.FLAG_RECEIVER_REGISTERED_ONLY | Intent.FLAG_RECEIVER_FOREGROUND