我想創建一個應用程序,當通過使用brodcast接收器接收或發送短信時發出通知。如何操作?謝謝。短信推送通知
短信推送通知
回答
試試這個 公共類smsActivity擴展廣播接收器{
protected static final String TAG = "PlayingAudio";
@Override
public void onReceive(Context context, Intent intent)
{
this.mContext = context;
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecordtest.3gp";
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
incomingnum=msgs[i].getOriginatingAddress();
MsgBody=msgs[i].getMessageBody().toString();
//str += "SMS from " +incomingnum;
//str += " :";
str += MsgBody;
// str += "\n";
System.out.println("Str is "+str);
}
Notification notification = new Notification(android.R.drawable.ic_popup_reminder,
"My Notification", System.currentTimeMillis());
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;*/
}
}
}
}
謝謝你,但代碼沒有運行。 – swanand 2012-03-07 12:26:07
在模擬器測試運行的API 7映像。
package add.yourpackage.here;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
StringBuilder smsSummary = new StringBuilder("SMS received from: ");
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
final String sender = msgs[i].getOriginatingAddress();
final String message = msgs[i].getMessageBody().toString();
smsSummary.append(sender);
smsSummary.append("; ").append(message).append("\n");
Log.d("SMS_RECEIVER", "Str is " + smsSummary);
}
NotificationManager notifManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notif = new Notification(
android.R.drawable.ic_popup_reminder, smsSummary,
System.currentTimeMillis());
notif.defaults |= Notification.DEFAULT_SOUND;
notif.defaults |= Notification.DEFAULT_VIBRATE;
notif.defaults |= Notification.DEFAULT_LIGHTS;
// The notification will be canceled when clicked by the user...
notif.flags |= Notification.FLAG_AUTO_CANCEL;
// ...but we still need to provide and intent; an empty one will
// suffice. Alter for your own app's requirement.
Intent notificationIntent = new Intent();
PendingIntent pi = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notif.setLatestEventInfo(context, "SMS Notification",
"Another txt for you", pi);
notifManager.notify(0, notif);
}
}
}
如果您運行的API 11+可以使用Notification.Builder代替,但我的手機仍然在薑餅:
接收器類。
接收者的清單條目。您的應用程序仍然需要界定以短信的權限,但我相信你已經這樣做了:
<receiver
android:name=".service.SmsReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
</intent-filter>
</receiver>
我在API 8上工作。 – swanand 2012-03-24 14:19:39
沒關係,這段代碼對於API 8也可以正常工作:-)。我只在7上進行了測試,因爲它是我安裝的最低的API,並且您在原始問題中沒有陳述一個。 – Chilledrat 2012-03-24 16:30:48
- 1. 推送短信通知Mobilefirst
- 2. 短信通知
- 3. 訪問iPhone未接電話,短信,推送通知等
- 4. 當收到電子郵件/短信時推送通知
- 5. Android如何發送Flash(通知)短信?
- 6. 如何讓短信發送通知?
- 7. 推送推送通知
- 8. 推送通知
- 9. 推送通知
- 10. 推送通知
- 11. 推送通知
- 12. 推送通知
- 13. 推送通知
- 14. 推送通知
- 15. 在Android中通過短信通道獲取SMS短信發送通知
- 16. 通過PHP發送短信/短信
- 17. 「暖」推出推送通知
- 18. Android - 推送通知
- 19. Linphone推送通知
- 20. OnsenUI:推送通知
- 21. 從推送通知
- 22. 推送通知 - Android
- 23. PubNub推送通知
- 24. 推送通知下
- 25. 推送通知Genexus
- 26. 推送通知android
- 27. woklight推送通知
- 28. FCM推送通知
- 29. didRegisterForRemoteNotificationsWithDeviceToken - 推送通知
- 30. 推送通知7.1
是否有差異http://stackoverflow.com/questions/4117701/android-sms-broadcast - 接收器? – Tim 2012-03-05 10:58:00