Android notification onclick



我想知道如何实现Android通知的onClickListener。我正在尝试在通知中实现sendText(),而不是将用户发送到主要活动:

public class AlertReceiver extends BroadcastReceiver {
    Context mContext;
    String number;
    String messageList;
    String name;
    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        name = intent.getStringExtra("name");
        messageList = intent.getStringExtra("messageList");
        number = intent.getStringExtra("number");
        createNotification(context, "times up " + name, "5 seconds passed!", "alert");
    }
    public void createNotification(Context context, String message, String messageText, String messageAlert){
        PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(message)
                .setTicker(messageText)
                .setContentText(messageAlert);
        mBuilder.setContentIntent(notificIntent);
        mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());
    }
    public void sendText(){

        //Turn string of all messages into an ArrayList in order to get one specific message at random
        ArrayList<String> messagesArrayList = null;
        try {
            messagesArrayList = Utility.getArrayListFromJSONString(messageList);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Random rand = new Random();
        //todo the following may cause a bug if there are no messages in list
        int  n = rand.nextInt(messagesArrayList.size());

        String message = messagesArrayList.get(n);
        try {
            //send text message
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(number, null, message, null, null);
            Toast.makeText(mContext, "Message Sent",
                    Toast.LENGTH_SHORT).show();
        } catch (Exception ex) {
            //If text message wasn't sent attempt to send text another way (through the user's text messaging app)
            // Most likely due to text message permissions not being accepted by user
            Intent intent = new Intent(Intent.ACTION_SENDTO);
            intent.setData(Uri.parse("smsto:" + number));  // This ensures only SMS apps respond
            intent.putExtra("sms_body", message);
            if (intent.resolveActivity(mContext.getPackageManager()) != null) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(intent);
            }
        }
    }
}

注意以下信息并不是真正必要的。这主要是因为Stackoverflow认为我的代码与文本比率太低,但也可能有助于澄清一下:

sendText()基本上是一种方法,该方法试图在不打开新活动的情况下发送预制作的短信。但是,如果没有权限,它将使用意图打开新活动。因此,为了最大程度地减少即将出现的屏幕数量,并最简单地使用sendtext方法来完成它。

,而不是创建一个悬浮仪来启动活动,您可以创建一个悬浮材料来启动广播接收器,如下所示

PendingIntent intent = PendingIntent.getBroadcast(context, 0, new Intent(context, SendTextReceiver.class), 0);

因此,当您单击通知时,它将调用您的广播接收器 sendtextreceiver 并在其中进行发件逻辑,因此,这样您就不必始终启动活动,并且您的逻辑将不做,而无需完成。活动

如果您不想将用户发送到活动,则可以触发服务:PendingIntent.getService(...)当用户单击通知并执行工作以将文本发送到此处时。

尝试以下:

    public class AlarmReceiver extends BroadcastReceiver {
 private static final int MY_NOTIFICATION_ID=1;
 NotificationManager notificationManager;
 Notification myNotification;
 @Override
 public void onReceive(Context context, Intent intent) {
     // here DoSomething is your service name that you want to start
     Intent myIntent = new Intent(context, DoSomething.class);
     PendingIntent pendingIntent = PendingIntent.getService(
            context, 
            0, 
            myIntent, 
            0);
     myNotification = new NotificationCompat.Builder(context)
       .setContentTitle("Exercise of Notification!")
       .setContentText("Do Something...")
       .setTicker("Notification!")
       .setWhen(System.currentTimeMillis())
       .setContentIntent(pendingIntent)
       .setDefaults(Notification.DEFAULT_SOUND)
       .setAutoCancel(true)
       .setSmallIcon(R.drawable.ic_launcher)
       .build();
     notificationManager = 
       (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
     notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
 }
}

相关内容

  • 没有找到相关文章

最新更新