我在學習使用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;
}
}
http://stackoverflow.com/questions/7714731/broadcastreceiver-for-screen-lock-沒有被觸發你檢查 –
謝謝它的作品。我在 –