我正在尝试编写一个应用程序来拦截即将发布的短信。
工作正常。
现在,当我尝试为其设置自己的通知时,它无法显示。
以下是代码:
package sms;
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.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.telephony.gsm.SmsMessage;
import android.util.Log;
import com.pbc.connect2.StartActivity;
@SuppressWarnings("deprecation")
public class IncomingSms extends BroadcastReceiver {
// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();
@Override
public void onReceive(Context context, Intent intent) {
this.abortBroadcast();
// TODO Auto-generated method stub
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
showNotification("senderNum: "+ senderNum + ", message: " + message, "New Msg", context);
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
private void showNotification(String eventtext, String Title, Context ctx) {
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
// Set the icon, scrolling text and timestamp
@SuppressWarnings("deprecation")
Notification notification = new Notification(0,
"Alert", System.currentTimeMillis());
// The PendingIntent to launch our activity if the user selects this
// notification
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
new Intent(ctx, StartActivity.class), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(ctx, Title, eventtext,
contentIntent);
notification.sound = alarmSound;
notification.ledOnMS = 5;
// Send the notification.
notificationManager.notify("Title", 0, notification);
}
}
我已经通过代码调试了,我看到它们正在执行。但通知并未显示。
showNotification()函数不起作用,我得到系统默认通知。我哪里错了? 一些帮助将不胜感激。