0

我想知道如何實現一個android通知的onClickListener。我想實現的,而不是發送用戶的主要活動通知sendText()Android通知onclick

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方法來實現。

回答

1

如果您不想將用戶發送到活動,那麼當用戶單擊通知並執行作業以在其中發送文本時,您可以觸發服務:PendingIntent.getService(...)

0

試試這個:

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

} 
0

不用創建的PendingIntent啓動一個活動,你可以創建一個的PendingIntent消防廣播接收器如下圖所示

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

所以當您單擊通知,它會調用您的BroadCast接收器SendTextReceiver並在其中執行您的sendText邏輯,所以通過這種方式您不必始終開始一個活動,並且您的邏輯將在沒有活動的情況下完成