2013-07-04 229 views
0

我在學習使用Android BroadcasReceiver。 我寫了這段代碼,但它不工作...我嘗試了例如改變時間等... 這是什麼錯誤?爲什麼我的BroadcastReceiver不起作用?

我在清單補充說:

<receiver android:name="com.example.broadcastreceiverspike.Broadcast" > 
     <intent-filter android:priority="100"> 
      <action android:name="android.intent.action.ACTION_SCREEN_ON" /> 
      <action android:name="android.intent.action.ACTION_SCREEN_OFF" /> 
      <action android:name="android.intent.action.TIME_SET" /> 
     </intent-filter> 
</receiver> 

我簡單BroadcasReceiver:

public class Broadcast extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      Log.d("BROADCAST", "It worked"); 
      Toast.makeText(context, "BROADCAST", Toast.LENGTH_LONG).show(); 
     } 
} 

我的主要活動(默認的主活動)

public class MainActivity extends Activity { 

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

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 
+1

http://stackoverflow.com/questions/7714731/broadcastreceiver-for-screen-lock-沒有被觸發你檢查 –

+1

謝謝它的作品。我在 –

回答

1

我解決了!

「與其他廣泛流傳的意圖不同,對於Intent.ACTION_SCREEN_OFF和Intent.ACTION_SCREEN_ON,您無法在Android清單中聲明它們!我不知道確切原因,但他們必須在IntentFilter的在Java代碼中」

http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/

清單登記

<receiver android:name=".Broadcast" > 
     <intent-filter>     
      <action android:name="android.intent.action.TIME_SET" /> 

     </intent-filter> 
    </receiver> 

主要活動類

公共類MainActivity擴展活動{

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

    // INITIALIZE RECEIVER 
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); 
    filter.addAction(Intent.ACTION_SCREEN_OFF); 
    BroadcastReceiver mReceiver = new Broadcast(); 
    registerReceiver(mReceiver, filter); 

} 

}

我的廣播接收機

公共類廣播延伸的BroadcastReceiver {

public static String TAG = "BROADCAST"; 

@Override 
public void onReceive(Context context, Intent intent) { 

    if (intent.getAction().equals(Intent.ACTION_TIME_CHANGED)) 
    { 
     Log.d(TAG, "BROADCAST Cambio di orario"); 
     Toast.makeText(context, "BROADCAST Cambio di orario", Toast.LENGTH_LONG).show(); 
    } 
    else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { 
     // DO WHATEVER YOU NEED TO DO HERE 
     Log.d(TAG, "BROADCAST Screen OFF"); 
     Toast.makeText(context, "Screen OFF", Toast.LENGTH_LONG).show(); 
    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { 
     // AND DO WHATEVER YOU NEED TO DO HERE 
     Log.d(TAG, "BROADCAST Screen ON"); 
     Toast.makeText(context, "BROADCAST Screen ON", Toast.LENGTH_LONG).show(); 
    } 
} 

}

+0

該文檔可能有一些錯誤,我已經開了兩天頭,並且我發現使用READ_PHONE_STATE權限,我的廣播可以工作! (從4.3到5.1測試),請參閱下面的答案和代碼,這裏不適合。 – Josh

0

退房this tutorial關於廣播接收機。

更新: 我不知道你正在使用本教程中,因爲有很多有用的東西在本教程中,像這樣:

@Override 
public void onResume() { 
    super.onResume(); 

    // Register mMessageReceiver to receive messages. 
    LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, 
     new IntentFilter("my-event")); 
    } 

    // handler for received Intents for the "my-event" event 
    private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
     // Extract data included in the Intent 
     String message = intent.getStringExtra("message"); 
     Log.d("receiver", "Got message: " + message); 
    } 
}; 

@Override 
protected void onPause() { 
    // Unregister since the activity is not visible 
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver); 
    super.onPause(); 
} 

換句話說,你已經創建一個自定義broadcast receiver class,但你沒有使用它。

也請檢查this

+0

下添加了我的解決方案我正在使用本教程... –

+0

我已更新我的答案.. –

0

您是否添加了所需的錄像?

使用的許可機器人:名稱=「android.permission.READ_PHONE_STATE」

這是一個接收器來處理屏幕關閉時或上或鎖定:

public class ScreenReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) 
{ 
    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) 
    { 



    } 
    else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) 
    { 


    } 
    else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)) 
    { 

    } 

} 

} 這是清單:

<receiver android:name=".ScreenReceiver"> 
     <intent-filter> 
      <action android:name="android.intent.action.USER_PRESENT" />   
    <action android:name="android.intent.action.SCREEN_OFF" /> 
    <action android:name="android.intent.action.SCREEN_ON" /> 

     </intent-filter> 
    </receiver> 
+0

爲什麼添加android.intent.action.USER_PRESENT?我不使用它 –

+0

我想知道什麼時候鎖屏已經啓動。然後我顯示一張圖片。因爲我不想永遠離開它。我需要知道用戶何時在場。 –

+0

這是否適合你? –

0

我有同樣的問題,我固定它(在4.3和5.1測試)。我能夠在清單中聲明「android.intent.action.USER_PRESENT」,只要你有READ_PHONE_STATE權限,那就OK!我的迷你應用程序由一個廣播接收器組成,可以響應屏幕開/關狀態,並運行連續語音識別的後臺服務。如果屏幕關閉,則關閉識別功能。下面是代碼,即可享受:清單:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> <receiver android:name="classes.VoiceLaunchReceiver" > 
      <intent-filter>     
       <action android:name="android.intent.action.USER_PRESENT" />  
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </receiver> 

廣播接收器:

public class VoiceLaunchReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context ctx, Intent intent) {  
     Intent service = new Intent(ctx, VoiceLaunchService.class); 
    // service.putExtra(action, true); 
     Log.i("joscsr","Incoming Voice Launch Broadcast..."); 

     if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) { 
      Log.i("joshcsr", "************\nCSR Resumed (BC)\n************"); 
      ctx.startService(service); 
      } 
     if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { 
      Log.i("joshcsr", "************\nCSR STOPPED by SCREEN (BC)\n************"); 
      ctx.stopService(service); 
      } 
     } 
} 

你可以想像,我USER_PRESENT廣播接收器沒有註冊任何其他地方。我在我的服務的onCreate方法中註冊了ACTION_SCREEN_OFF和ON,這是我的接收方觸發的。

@Override 
public void onCreate() { 
    super.onCreate(); 
    //Register screen ON/OFF BroadCast 
    launcher=new VoiceLaunchReceiver(); 
    IntentFilter i=new IntentFilter(Intent.ACTION_SCREEN_OFF); 
    i.addAction(Intent.ACTION_SCREEN_ON);    
    registerReceiver(launcher,i); 
    Log.d("joshcsr","VoiceLaunch Service CREATED"); 
    } 

最後我註銷屏幕開/關中的onDestroy()我公司的服務:

@Override 
    public void onDestroy() { 
     super.onDestroy(); 
     unregisterReceiver(launcher);}