【问题标题】:Android Show Activity above Lockscreen on Click of Notification IconAndroid 在单击通知图标时在锁屏上方显示活动
【发布时间】:2016-08-17 17:54:03
【问题描述】:

我们需要显示信息而不需要显示解锁屏幕。 我们可以通过以下代码正常执行此操作:

WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON

但在这种情况下,它应该点击通知栏中的通知图标。如何才能做到这一点?


我有一个服务正在运行,它在屏幕解锁后工作正常。它在图标单击时打开活动。但是当屏幕被锁定时我无法做到这一点。

public class notifysrvc extends  Service {
    @Override
    public void onCreate() {
        super.onCreate();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        showRecordingNotification();
        return Service.START_STICKY;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service Destroy", Toast.LENGTH_LONG).show();
    }
    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
    private void showRecordingNotification() {
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.lifegaurdicon12)
                        .setContentTitle("Emergency")
                        .setOngoing(true)
                        .setPriority(Notification.PRIORITY_MAX)
                        .setContentText("Click here!");
// Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(this, Menu.class);
//Tried this add flags to make it work
resultIntent.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(Menu.class);
// Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());
    }
}

【问题讨论】:

    标签: android android-notifications lockscreen notificationmanager


    【解决方案1】:
    I tried it using remoteviews as suggested in few examples but it just doesn't work.
    I tried this but it  just doesn't work
    
            // NOTE: while creating pendingIntent: requestcode must be different!
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                    .setVisibility(Notification.VISIBILITY_PUBLIC)
                    .setSmallIcon(R.drawable.ic_launcher).setContentTitle("My Title")
                    .setContentText("Service running in the background")
                    .setOngoing(true);
            Intent openIntent = new Intent(this, MainActivity.class);
    
    
            PendingIntent pOpenIntent = PendingIntent.getActivity(this, 0, openIntent, 0);
            mBuilder.setContentIntent(pOpenIntent);
            // Gets an instance of the NotificationManager service
            NotificationManager mNotifyMgr =
                    (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // Builds the notification and issues it.
            mNotifyMgr.notify(1090, mBuilder.build());
    // Notification with exit button if supported
            String ACTION_NOTIFICATION_EXITACTIVITY = "com.example.circle.myapplication.shownotifyactivity";
            Intent exitIntent = new Intent();
            exitIntent.setAction(ACTION_NOTIFICATION_EXITACTIVITY);
            PendingIntent pExitIntent = PendingIntent.getBroadcast(this, 1, exitIntent, 0);
            RemoteViews view = new RemoteViews(getPackageName(), R.layout.activity_shownotifyactivity);
            view.setOnClickPendingIntent(R.id.notification_click, pExitIntent);
            mBuilder.setContent(view);
    I tried the example [http://stackoverflow.com/questions/27673943/notification-action-button-not-clickable-in-lock-screen?answertab=active#tab-top][1]
    
    
      [1]: http://stackoverflow.com/questions/27673943/notification-action-button-not-clickable-in-lock-screen?answertab=active#tab-top
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-06
      • 2020-06-17
      • 1970-01-01
      • 2020-01-12
      • 2021-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多