Android 待决意向通知不起作用

Android 待决意向通知不起作用,android,android-intent,android-notifications,android-pendingintent,heads-up-notifications,Android,Android Intent,Android Notifications,Android Pendingintent,Heads Up Notifications,我正在尝试设置一个带有按钮的提示通知,当单击按钮时,会在后台发送sms,或者如果单击实际通知,则会打开一个活动(这部分我可以完成,没有问题)。 这是我下面的代码,但不确定我是否用正确的方法 主要活动: private void sendNotifaction(){ // Create an explicit content Intent that starts the main Activity. Intent notificationIntent = new Intent(t

我正在尝试设置一个带有按钮的提示通知,当单击按钮时,会在后台发送sms,或者如果单击实际通知,则会打开一个活动(这部分我可以完成,没有问题)。 这是我下面的代码,但不确定我是否用正确的方法

主要活动

private void sendNotifaction(){

    // Create an explicit content Intent that starts the main Activity.
    Intent notificationIntent = new Intent(this, sendSmsService.class);

    // Construct a task stack.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

    // Add the main Activity to the task stack as the parent.
    stackBuilder.addParentStack(MainActivity.class);

    // Push the content Intent onto the stack.
    stackBuilder.addNextIntent(notificationIntent);

    // Get a PendingIntent containing the entire back stack.
    PendingIntent notificationPendingIntent =
           stackBuilder.getPendingIntent(1, PendingIntent.FLAG_UPDATE_CURRENT);


    // Get a notification builder that's compatible with platform versions >= 4
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    // Define the notification settings.
    builder.setSmallIcon(R.mipmap.ic_launcher)
            // In a real app, you may want to use a library like Volley
            // to decode the Bitmap.
            .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                    R.mipmap.ic_launcher))
            .setColor(Color.RED)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setVibrate(new long[]{10,10,10})
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setContentTitle("Noti")
            .setContentText(getString(R.string.geofence_transition_notification_text))
            .addAction(R.mipmap.ic_launcher,"Send",notificationPendingIntent)

            .setContentIntent(notificationPendingIntent);


    // Dismiss notification once the user touches it.
    builder.setAutoCancel(true);

    // Get an instance of the Notification manager
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Issue the notification
    mNotificationManager.notify(0, builder.build());
}
这是INTERNTSERVICE SENDSMSSSERVICE,我认为我出了问题

sendSmsService

public class sendSmsService extends IntentService {


String senderNum = "";
String sms = "";

/**
 * Creates an IntentService.  Invoked by your subclass's constructor.
 *
 * @param name Used to name the worker thread, important only for debugging.
 */
public sendSmsService(String name) {
    super(name);
}

@Override
protected void onHandleIntent(Intent intent) {

    try {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(senderNum, null, sms, null, null);

        Toast.makeText(getApplicationContext(), "Sms Sent", Toast.LENGTH_LONG).show();

    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), "Sms Failed", Toast.LENGTH_LONG).show();
    }

}
}

它似乎根本没有启动on句柄中的代码。

我明白了!:) 在阅读了Vogella关于广播接收机的本页之后 我把第二节课改成广播接收机。 并对通知方式作了相关修改

sendSmsReceiver

public class sendSmsReceiver extends BroadcastReceiver{


String senderNum = "";
String sms = "";


@Override
public void onReceive(Context context, Intent intent) {
    try {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(senderNum, null, sms, null, null);

        Toast.makeText(context, "Sms Sent", Toast.LENGTH_LONG).show();

    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(context, "Sms Failed", Toast.LENGTH_LONG).show();
    }
}
}

main活动

private void headsUpSmsNotifaction(String notificationDetails){

    // Create an explicit content Intent that starts the main Activity.
    Intent notificationIntent = new Intent(this, sendSmsReceiver.class);
    PendingIntent notificationPendingIntent = PendingIntent.getBroadcast(this,44,notificationIntent,0);


    // Get a notification builder that's compatible with platform versions >= 4
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    // Define the notification settings.
    builder.setSmallIcon(R.mipmap.ic_launcher)
            // In a real app, you may want to use a library like Volley
            // to decode the Bitmap.
            .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                    R.mipmap.ic_launcher))
            .setColor(Color.RED)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setVibrate(new long[]{10,10,10})
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setContentTitle(notificationDetails)
            .setContentText(getString(R.string.geofence_transition_notification_text))
            .addAction(R.mipmap.ic_launcher,"Send",notificationPendingIntent)

            .setContentIntent(notificationPendingIntent);


    // Dismiss notification once the user touches it.
    builder.setAutoCancel(true);

    // Get an instance of the Notification manager
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Issue the notification
    mNotificationManager.notify(0, builder.build());
}
我明白了!:) 在阅读了Vogella关于广播接收机的本页之后 我把第二节课改成广播接收机。 并对通知方式作了相关修改

sendSmsReceiver

public class sendSmsReceiver extends BroadcastReceiver{


String senderNum = "";
String sms = "";


@Override
public void onReceive(Context context, Intent intent) {
    try {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(senderNum, null, sms, null, null);

        Toast.makeText(context, "Sms Sent", Toast.LENGTH_LONG).show();

    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(context, "Sms Failed", Toast.LENGTH_LONG).show();
    }
}
}

main活动

private void headsUpSmsNotifaction(String notificationDetails){

    // Create an explicit content Intent that starts the main Activity.
    Intent notificationIntent = new Intent(this, sendSmsReceiver.class);
    PendingIntent notificationPendingIntent = PendingIntent.getBroadcast(this,44,notificationIntent,0);


    // Get a notification builder that's compatible with platform versions >= 4
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    // Define the notification settings.
    builder.setSmallIcon(R.mipmap.ic_launcher)
            // In a real app, you may want to use a library like Volley
            // to decode the Bitmap.
            .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                    R.mipmap.ic_launcher))
            .setColor(Color.RED)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setVibrate(new long[]{10,10,10})
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setContentTitle(notificationDetails)
            .setContentText(getString(R.string.geofence_transition_notification_text))
            .addAction(R.mipmap.ic_launcher,"Send",notificationPendingIntent)

            .setContentIntent(notificationPendingIntent);


    // Dismiss notification once the user touches it.
    builder.setAutoCancel(true);

    // Get an instance of the Notification manager
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Issue the notification
    mNotificationManager.notify(0, builder.build());
}