Android 中传入短信的状态栏通知?
Status bar notification on incoming SMS in Android?
我是 Android 编程的初学者。我正在尝试在 android 设备中实现传入短信通知。但是,我能够为相同但无法实现状态栏通知创建吐司。
谁能帮帮我?
现在我编写了以下代码:
@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);
}
}
}
}
}
为什么要在方法中创建 activity?应该是
@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());
}
}
我是 Android 编程的初学者。我正在尝试在 android 设备中实现传入短信通知。但是,我能够为相同但无法实现状态栏通知创建吐司。 谁能帮帮我?
现在我编写了以下代码:
@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);
}
}
}
}
}
为什么要在方法中创建 activity?应该是
@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());
}
}