【问题标题】:Android notification onclickAndroid 通知 onclick
【发布时间】:2016-12-30 10:58:12
【问题描述】:

我想知道如何为 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 方法来实现。

【问题讨论】:

    标签: android android-intent notifications android-pendingintent


    【解决方案1】:

    您可以创建一个pendingIntent 来触发广播接收器,而不是创建一个pendingIntent 来启动一个Activity,如下所示

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

    所以当您单击通知时,它会调用您的广播接收器 SendTextReceiver 并在其中执行您的 sendText 逻辑,因此通过这种方式您不必总是启动一个活动,您的逻辑将在没有的情况下完成一个活动

    【讨论】:

      【解决方案2】:

      如果您不想将用户发送到 Activity,则可以在用户单击通知时触发服务:PendingIntent.getService(...) 并执行将文本发送到那里的工作。

      【讨论】:

        【解决方案3】:

        试试这个:

            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);
         }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多