2
A
回答
3
要麼你可以做兩件事情:
- 的創建和
Manifest
- 定義
BroadcastReceiver
創建並在代碼註冊BroadcastReceiver
。
選項2(你問):
創建代碼BroadcastReceiver
(MyBroadcastReceiver)。 申報MyBroadcastReceiver在Activity
範圍:
MyBroadcastReceiver mMyBroadcastReceiver;
由註冊BroadcastReceiver
在Activity
:
IntentFilter filter = new IntentFilter(android.provider.Telephony.SMS_RECEIVED);
this.registerReceiver(mMyBroadcastReceiver, filter);
+0
這說「無法解析SMS_RECEIVED」 –
2
爲了有一個服務接收短信遵循下面的步驟:
- 更新清單給你的應用程序的權限,以接收短信 (WRITE_SMS,READ_SMS,RECEIVE_SMS)
- 沒有更新清單與接收機意向過濾器! (這 每個樣本代碼在網上似乎做)
在你的服務,你的服務類
private class SMSreceiver extends BroadcastReceiver { private final String TAG = this.getClass().getSimpleName(); @Override public void onReceive(Context context, Intent intent) { Bundle extras = intent.getExtras(); String strMessage = ""; if (extras != null) { Object[] smsextras = (Object[]) extras.get("pdus"); for (int i = 0; i < smsextras.length; i++) { SmsMessage smsmsg = SmsMessage.createFromPdu((byte[])smsextras[i]); String strMsgBody = smsmsg.getMessageBody().toString(); String strMsgSrc = smsmsg.getOriginatingAddress(); strMessage += "SMS from " + strMsgSrc + " : " + strMsgBody; Log.i(TAG, strMessage); } } } }
內創建一個嵌套的BroadcastReceiver類服務類,登記接收
android.provider.Telephony.SMS_RECEIVED
意圖過濾器:public class ServiceCommunicator extends Service { private SMSreceiver mSMSreceiver; private IntentFilter mIntentFilter; @Override public void onCreate() { super.onCreate(); //SMS event receiver mSMSreceiver = new SMSreceiver(); mIntentFilter = new IntentFilter(); mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED"); registerReceiver(mSMSreceiver, mIntentFilter); } @Override public void onDestroy() { super.onDestroy(); // Unregister the SMS receiver unregisterReceiver(mSMSreceiver); } }
這就是它!
注: 包住你想知道爲什麼我沒有從單獨的廣播接收器類中綁定到我的服務 - 這是行不通的,因爲bindService()
不可用。
0
按照您的要求進行註冊BroadcastReceiver
您MainActivity
適合於registerReceiver-codewise-inside-MainActivity
代碼如下==>
public class MainActivity extends Activity
{
BroadcastReceiver smsReceiver;
private IntentFilter myFilter = new IntentFilter(android.provider.Telephony.SMS_RECEIVED);
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
registerReceiver(smsReceiver, myFilter); // register BroadCastReceiver at time of initialization of Activity with proper Intent-Filter
}
@Override
protected void onPause()
{
unregisterReceiver(smsReceiver); // UnRegister BroadCastReceiver as you no longer have your activity at Foreground -- Saving CPU & Battery Drainage
super.onPause();
}
@Override
protected void onResume()
{
registerReceiver(smsReceiver, myFilter); // Register BroadCastReceiver Once again as your activity comes from pause to forground state again.
super.onResume();
}
smsReceiver = new BroadcastReceiver() //Implementation of your BroadCastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// Do whatever you like as sms is received and caught by these BroadCastReceiver
Toast.makeText(context, "SMS Received", Toast.LENGTH_LONG).show();
}
};
}
0
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//some action
}
};
IntentFilter filter = new IntentFilter("YOUR_ACTION");
registerReceiver(receiver, filter);
希望這有助於
1
對於API級別爲19及以上
IntentFilter smsFilter = new IntentFilter();
smsFilter.addAction(Telephony.Sms.Intents.SMS_RECEIVED_ACTION);
registerReceiver(this.receiver, filter);
相關問題
- 1. 如何在活動中取消註冊接收者?
- 2. 動態註冊接收者
- 3. 如何避免接收者在活動中註冊時廣播被浪費?
- 4. 無法動態註冊接收者
- 5. 取消註冊在活動中註冊的所有廣播接收機
- 6. 如何在TaskManager中註冊活動?
- 7. 如何在manifest.xml中註冊活動
- 8. 清單與活動中的廣播接收器註冊
- 9. 活動註冊
- 10. 我如何發送註冊ID從GCM在broadcastReceiver獲取主要活動
- 11. Android - 何時註冊/取消註冊在活動中創建的廣播接收器?
- 12. 如何在主要活動中直接顯示動態壁紙?
- 13. 的Android無法暫停活動接收器未註冊
- 14. 如何註冊廣播接收器?
- 15. 一個主要活動調用一個意圖和原始接收者被殺
- 16. 何時取消註冊onCreate中註冊的廣播接收器?
- 17. 在OSGi中註冊servlet接收參數
- 18. 動態註冊BroadcastReceiver - 接收器未註冊
- 19. 動態註冊廣播接收器
- 20. 我應該在哪裏註銷我的視圖中的註冊接收者?
- 21. FragmentActivity:在哪裏註冊接收者,打開數據庫
- 22. 主幹需要js註冊
- 23. 動態活動註冊
- 24. 如何在其他claas的主要活動中調用活動?
- 25. 如何設置django註冊帳戶需要手動激活?
- 26. 接收器未註冊
- 27. 服務註冊接收器
- 28. LocalBroadcastManager - 接收器未註冊
- 29. 註冊BOOT_COMPLETED接收器8.2
- 30. ViewFlipper:接收未註冊
你可以看看這個帖子的http://計算器。com/questions/4660823/android-alarm-not-working/4661122#4661122 – ccheneson
關於此主題有很多答案。檢查[this](http://stackoverflow.com/a/4805733/1521064),[this](http://stackoverflow.com/questions/4134203/how-to-use-registerreceiver-method)和[this] (http://stackoverflow.com/questions/6508300/how-to-register-broadcast-receiver)。 – vault
[以編程方式註冊廣播接收器]的可能的重複(http://stackoverflow.com/questions/4805269/programmatically-register-a-broadcast-receiver) – glomad