在android中设置关闭操作的关闭通知 [英] close notification with set up close action in android

查看:144
本文介绍了在android中设置关闭操作的关闭通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个操作的通知,

I have notification with 2 actions,

关闭按钮应关闭通知及其服务.

Close button should close notification and its service.

     public class TimerService extends Service {
        ...          

  private void setupNotification(String s) {
             Intent notificationIntent_Restore = new Intent(this,Adult1Activity.class);
             notificationIntent_Restore.putExtra(EXTRA_NOTE,NOTE_RESTORE);
             PendingIntent restoreIntent = PendingIntent.getActivity(this,
                     0, notificationIntent_Restore, PendingIntent.FLAG_UPDATE_CURRENT);

             final Intent notificationIntent_Close = new Intent(this, Adult1Activity.class);
             notificationIntent_Close.putExtra(EXTRA_NOTE, NOTE_CLOSE);
             PendingIntent closeIntent = PendingIntent.getActivity(this,
                     1, notificationIntent_Close, PendingIntent.FLAG_CANCEL_CURRENT);




             notification = new NotificationCompat.Builder(this)
                     .setSmallIcon(R.mipmap.ic_launcher)
                     .setLargeIcon(BitmapFactory.decodeResource( getResources(), R.mipmap.ic_launcher))
                     .setContentTitle("Stand Up!")
                     .setContentText(s)
                     .setAutoCancel(true)
                     .addAction(new NotificationCompat.Action(R.drawable.ic_note_close, "Close", closeIntent))
                     .addAction(new NotificationCompat.Action(R.drawable.ic_note_restore, "Restore", restoreIntent))
                     .setContentIntent(restoreIntent);

             notificationManager.notify(0, notification.build());
         }

我想用 CLOSE 操作关闭它.我在 AdultActivity 类中使​​用此代码:

I want to close it with CLOSE action. I use this code in AdultActivity class:

  @Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent); // Make sure to call super
    handleIntent(intent);
}

private void handleIntent(Intent intent) {
    final String a = intent.getStringExtra(EXTRA_NOTE);
    if (a != null) {
        switch (a) {
            case NOTE_RESTORE:
                //stopBTN.setVisibility(View.VISIBLE);
                break;

            case NOTE_CLOSE:
                stopService(serviceIntent);
                CloseApp();
                break;
        }
    }
}

最后我的应用程序停止了

finaly my app has stoped and

    01/25 11:45:05: Launching app
Cold swapped changes.
$ adb shell am start -n "standup.maxsoft.com.standup/standup.maxsoft.com.standup.help.HelpActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Connected to process 2777 on device emulator-5554
W/art: Suspending all threads took: 7.291ms
I/art: Background sticky concurrent mark sweep GC freed 11717(1506KB) AllocSpace objects, 45(768KB) LOS objects, 71% free, 1054KB/3MB, paused 8.582ms total 74.057ms
D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

                  [ 01-25 11:44:44.661  2777: 2777 D/         ]
                  HostConnection::get() New Host Connection established 0xa32f3910, tid 2777


                  [ 01-25 11:44:44.715  2777: 2798 D/         ]
                  HostConnection::get() New Host Connection established 0xa32f3ec0, tid 2798
I/OpenGLRenderer: Initialized EGL, version 1.4
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa32fec80, error=EGL_SUCCESS
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa32e2500, error=EGL_SUCCESS
E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab7d7d30
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa32e2aa0, error=EGL_SUCCESS
E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab7d8cf0
W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa32e2aa0, error=EGL_SUCCESS
I/art: Background partial concurrent mark sweep GC freed 4628(216KB) AllocSpace objects, 5(200KB) LOS objects, 40% free, 3MB/5MB, paused 12.525ms total 31.801ms
E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb4013960
E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab7d8dd0
Application terminated.

我能做什么?

public class TimerService extends Service {
    public CountingDownTimer countingDownTimer;
    public static int totalSeconds;
    public static String timeString=null;
    public static long seconds_for_compare;


    public SharedPreferences sharedPreferences;

     public static NotificationCompat.Builder notification;
    public static NotificationManager notificationManager;
    private static final String EXTRA_NOTE = "NOTE";
    private static final String NOTE_RESTORE = "restore";
    private static final String NOTE_CLOSE = "close";



    private static final String ACTION_CLOSE = "ACTION_CLOSE";


    @Override
    public void onCreate() {

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        countingDownTimer = new CountingDownTimer(totalSeconds,500); //milliseconds , (500 means) onTick function will be called at every 500
        countingDownTimer.start();

        notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        setupNotification(TimerService.timeString);

        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        if (intent != null) {
            final String action = intent.getAction();
            switch (action) {
                case ACTION_CLOSE:
                    stopSelf();
                    break;
            }
        }
        // return START_NOT_STICKY;
         return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        countingDownTimer.cancel();
        notificationManager.cancelAll();
        Adult1Activity.onFinish();
    }


    //****************Notification ********************
     private void setupNotification(String s) {

         Intent notificationIntent_Restore = new Intent(this,Adult1Activity.class);
         notificationIntent_Restore.putExtra(EXTRA_NOTE,NOTE_RESTORE);
         PendingIntent restoreIntent = PendingIntent.getActivity(this,
                 0, notificationIntent_Restore, PendingIntent.FLAG_UPDATE_CURRENT);

         final Intent notificationIntent_Close = new Intent(this, TimerService.class);
         notificationIntent_Close.setAction(ACTION_CLOSE); // use Action
         PendingIntent closeIntent = PendingIntent.getService(this,
                 1, notificationIntent_Close, PendingIntent.FLAG_UPDATE_CURRENT);


         notification = new NotificationCompat.Builder(this)
                 .setSmallIcon(R.mipmap.ic_launcher)
                 .setLargeIcon(BitmapFactory.decodeResource( getResources(), R.mipmap.ic_launcher))
                 .setContentTitle("Stand Up!")
                 .setContentText(s)
                 .setAutoCancel(false)
                 .addAction(new NotificationCompat.Action(R.drawable.ic_note_close, "Close", closeIntent))
                 .addAction(new NotificationCompat.Action(R.drawable.ic_note_restore, "Restore", restoreIntent))
                 .setContentIntent(restoreIntent);

         notificationManager.notify(0, notification.build());
     }

推荐答案

如果你只是想从一个通知中停止一个 Service,你不需要打扰 Activity.

If you just want to stop a Service from a Notification, you don't need to bother Activity.

在服务中,定义关闭操作:

In a Service, define an action for closing:

private static final String ACTION_CLOSE = "ACTION_CLOSE";

// in setupNotification():

final Intent notificationIntent_Close = new Intent(this, TimerService.class);
notificationIntent_Close.setAction(ACTION_CLOSE); // use Action
PendingIntent closeIntent = PendingIntent.getService(this,
        1, notificationIntent_Close, PendingIntent.FLAG_UPDATE_CURRENT);

在服务中,处理动作

@Override
public int onStartCommand(final Intent intent, final int flags, 
            final int startId) {
    if (intent != null) {
        final String action = intent.getAction();
        if (action != null) {
            switch (action) {
                case ACTION_CLOSE:
                    stopSelf();
                    break;
            }
        }
    }
    return START_NOT_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
    notificationManager.cancel(0);
}

这篇关于在android中设置关闭操作的关闭通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆