【发布时间】: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);
}
}
}
}
}
【问题讨论】:
-
你为什么要使用这样的活动?对我来说,弄清楚你哪里出错了,这甚至没有足够的意义。
-
这只是一次疯狂的尝试,因为我只是一个初学者。你能解释一下我应该如何获得状态栏通知吗?
标签: android notifications sms statusbar