android - 当用户收到短信时创建通知
问题描述
我想在收到特定短信时收到通知“A”或“B”我无法在广播接收中创建通知。那我该怎么办?
public class ReceiveMessage extends BroadcastReceiver {
final SmsManager mysms = SmsManager.getDefault();
final String ALERT="A";
final String ALERT2="B";
public void onReceive (Context context, Intent intent) {
Bundle myBundle = intent.getExtras();
try {
if (myBundle != null) {
final Object [] pdus = (Object[])myBundle.get("pdus");
String format = myBundle.getString("format");
for (int i = 0; i < pdus.length ; i++) {
SmsMessage mynewsms= SmsMessage.createFromPdu((byte[]) pdus[i], format);
String sms = mynewsms.getMessageBody();
Toast.makeText(context, "SMS"+mynewsms, Toast.LENGTH_SHORT).show();
if (sms.equals(ALERT) {
Toast.makeText(context,"............",Toast.LENGTH_LONG).show();
}
if (sms.equals(ALERT2) {
Toast.makeText(context,"Your child is out save zone",Toast.LENGTH_LONG).show();
}
}
}
} catch (Exception ex) {
}
}
}
解决方案
您可以像正常方式一样创建通知,无需像下面这样做任何额外的事情
public class ReceiveMessage extends BroadcastReceiver {
final SmsManager mysms = SmsManager.getDefault();
final String ALERT="A";
final String ALERT2="B";
public void onReceive (Context context, Intent intent) {
Bundle myBundle = intent.getExtras();
try {
if (myBundle != null) {
final Object [] pdus = (Object[])myBundle.get("pdus");
String format = myBundle.getString("format");
for (int i = 0; i < pdus.length ; i++) {
SmsMessage mynewsms= SmsMessage.createFromPdu((byte[]) pdus[i], format);
String sms = mynewsms.getMessageBody();
Toast.makeText(context, "SMS"+mynewsms, Toast.LENGTH_SHORT).show();
if (sms.equals(ALERT) {
Toast.makeText(context,"............",Toast.LENGTH_LONG).show();
showNotification(context,"Alert","............");
}
if (sms.equals(ALERT2) {
Toast.makeText(context,"Your child is out save zone",Toast.LENGTH_LONG).show();
showNotification(context,"Alert","Your child is out save zone");
}
}
}
} catch (Exception ex) {
}
}
}
public static void showNotification(Context context, String title, String msg) {
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent = new Intent();
PendingIntent pIntent = PendingIntent.getActivity(context, 2, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.BigTextStyle bigTextNotiStyle = null;
try {
bigTextNotiStyle = new NotificationCompat.BigTextStyle();
bigTextNotiStyle.setBigContentTitle(title);
bigTextNotiStyle.bigText("" + msg);
} catch (Exception e) {
e.printStackTrace();
}
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_noti_icon)
.setContentTitle("" + title)
.setContentText("" + msg)
.setStyle(bigTextNotiStyle)
.setAutoCancel(true)
.setColor(ContextCompat.getColor(context, R.color.staticbg))
.setContentIntent(pIntent)
.setLights(Color.GREEN, 3000, 3000);
mBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
mBuilder.setSound(soundUri); //Sound to play
notificationManager.notify(2, mBuilder.build());
}
推荐阅读
- php - 在登录期间在配置中设置动态数据库 - Laravel
- sql-server - 是否可以从变量插入表?
- python - 无法使用 dictConfig 配置过滤器
- java - 如何使用机器人在 Mac 上的 Android 模拟器上自动按键?
- swift - NSTableView:第二列不应该出现
- ruby-on-rails - 如何在上传到服务器之前压缩图像
- postgresql - 在 dockerized flask + postgres 应用程序中管理 pytest 测试的任何推荐方法?
- javascript - 在 iframe 中结合 2 个 onload 命令
- php - 将数组与公共键组合以进行 MYSQL 更新
- hash - 区块链散列中如何计算印章号?