无需打开活动即可收到特殊短信的通知

时间:2018-07-19 02:17:18

标签: android

我有一个可以读取SMS消息并通过notification进行通知的应用程序,但我想做到这一点而无需启动activity而只显示通知。

这是我的BroadcastReceiver代码:

public class SmsReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Bundle bundle = intent.getExtras();
    SmsMessage[] msg = null ;
    String msgStr = "";

    if ( bundle != null ){
        Object[] pdus = (Object[]) bundle.get ("pdus");
        msg = new SmsMessage [pdus.length] ;

        for( int i = 0; i<msg.length; i++){

            msg[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            msgStr = msg[i].getDisplayOriginatingAddress();

        }

        Intent smsIntent =new Intent(context,MainActivity.class);
        smsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        smsIntent.putExtra("msgStr",msgStr);
        context.startActivity(smsIntent);
    }

}

这是我的MainActivity代码:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent sms_intent = getIntent();
    Bundle b = sms_intent.getExtras();

    if( b != null ){
       notificationCall(b.getString("smsStr"),"message");
    }
}

public void notificationCall(String title , String message){

    NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
            .setDefaults(NotificationCompat.DEFAULT_ALL)
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentTitle(title+"\n")
            .setContentText(message);

  NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  notificationManager.notify(1,notificationBuilder.build());
}

}

2 个答案:

答案 0 :(得分:0)

您只需在广播接收器内移动notificationCall()方法即可。请记住,在Android O中,您需要按以下步骤创建Notification:

public void createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            CharSequence name = "Channel name";
            String description = "Description";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel("NOTIFICATION_CHANNEL", name, importance);
            channel.setDescription(description);
            NotificationManager notificationManager = getApplicationContext().getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
    }

以发布通知。更改通知的创建如下:

Notification notification = new NotificationCompat.Builder(this, "NOTIFICATION_CHANNEL")

代替:

NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)

答案 1 :(得分:0)

您需要创建服务,该服务将在没有任何活动的情况下运行,您可以阅读herehere