0

私はonClickListenerをAndroidの通知のために実装する方法を知りました。私の代わりに、メインの活動にユーザーを送信する通知に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

ユーザーをアクティビティに送信したくない場合は、ユーザーが通知をクリックしてそこにテキストを送信するときにサービスを起動できます。

0

はこれを試してみてください。代わりに活動を開始するpendingIntentを作成するあなたは、通知をクリックしたときに

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 intent = PendingIntent.getBroadcast(context, 0, new Intent(context, SendTextReceiver.class), 0); 

以下のように放送受信機を発射するpendingIntentを作成することができ、あなたのBroadCastレシーバーSendTextReceiverを呼び出し、その中にあなたのsendTextロジックを行います。このようにして、常にアクティビティを開始する必要はなく、あなたのロジックはアクティビティなしで行われます