Android中传入短信的状态栏通知?
Posted
技术标签:
【中文标题】Android中传入短信的状态栏通知?【英文标题】:Status bar notification on incoming SMS in Android? 【发布时间】:2015-05-10 08:02:54 【问题描述】:我是 android 编程的初学者。我正在尝试在 android 设备中实现传入 SMS 的通知。但是,我可以为相同的内容创建一个 toast,但无法实现状态栏通知。 谁能帮帮我?
现在我已经编写了以下代码:
@Override
public void onReceive(Context arg0, Intent arg1)
// Log.i(TAG,"OnReceive ++ ");
Bundle bndl = arg1.getExtras();
SmsMessage[] msg = null;
if (null != bndl)
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bndl.get("pdus");
msg = new SmsMessage[pdus.length];
for (int i=0; i<msg.length; i++)
msg[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS From " + msg[i].getOriginatingAddress();
str += " :\r\n";
str += msg[i].getMessageBody().toString();
str += "\n";
context = arg0;
//---display incoming SMS as a Android Toast---
Toast.makeText(arg0, str, Toast.LENGTH_SHORT).show();
@SuppressWarnings("unused")
class MynotificationMain extends Activity
int mNotificationId = 001;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_mynotification_main);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setContentTitle("New Message");
mBuilder.setContentText(str);
mBuilder.setTicker("New Message Alert!");
mBuilder.setSmallIcon(R.drawable.notification);
Intent resultIntent = new Intent(this, MainActivity.class );
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
MapsFragment obj = new MapsFragment();
obj.onLocationChanged(null);
【问题讨论】:
你为什么要使用这样的活动?对我来说,弄清楚你哪里出错了,这甚至没有足够的意义。 这只是一次疯狂的尝试,因为我只是一个初学者。你能解释一下我应该如何获得状态栏通知吗? 【参考方案1】:为什么要在方法中进行活动?应该是
@Override
public void onReceive(Context arg0, Intent arg1)
Bundle bndl = arg1.getExtras();
SmsMessage[] msg = null;
if (null != bndl)
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bndl.get("pdus");
msg = new SmsMessage[pdus.length];
for (int i=0; i<msg.length; i++)
msg[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS From " + msg[i].getOriginatingAddress();
str += " :\r\n";
str += msg[i].getMessageBody().toString();
str += "\n";
//---display incoming SMS as a Android Toast---
Toast.makeText(arg0, str, Toast.LENGTH_SHORT).show();
//---Make a notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setContentTitle("New Message");
mBuilder.setContentText(str);
mBuilder.setTicker("New Message Alert!");
mBuilder.setSmallIcon(R.drawable.notification);
Intent resultIntent = new Intent(this, MainActivity.class );
PendingIntent resultPendingIntent = PendingIntent.getActivity(arg0, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotifyMgr = (NotificationManager) arg0.getSystemService(arg0.NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
【讨论】:
问题是我的父类扩展了 BroadcastReceiver。我需要活动类来实现 getSystemService。 你的上下文为 arg0。然后只需调用 arg0.getSystemService 在扩展 BroadcastReceiver 的父类中实现通知代码时出现错误。有什么解决办法吗? 好的,首先它在 getActivity 中给了我错误:PendingIntent 类型中的方法 getActivity(Context, int, Intent, int) 不适用于参数 (MyBroadcastReceiver, int, Intent, int) ! 它在 NOTIFICATION_SERVICE 中给出错误:NOTIFICATION_SERVICE 无法解析为变量以上是关于Android中传入短信的状态栏通知?的主要内容,如果未能解决你的问题,请参考以下文章