2016-05-16 14 views
2

Ich möchte auf eine Benachrichtigung tippen können, wenn das Gerät gesperrt ist und eine Aktivität starten, ohne das Gerät zu entriegeln.Einführungs-Aktivität, wenn Benutzer auf eine Benachrichtigung vom Lockscreen tippt

Ich habe einige Fahnen auf die Aktivität in der onCreate() Methode, die die Aktivität angezeigt werden kann, wenn das Gerät gesperrt ist:

Window window = this.getWindow(); 
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); 
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); 
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); 
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 

Dies ist der Code, der die Benachrichtigung erstellt:

Intent intent = new Intent(this, MainActivity.class); 
PendingIntent pendingIntent = PendingIntent.getActivity(
     this, 
     0, 
     intent, 
     PendingIntent.FLAG_UPDATE_CURRENT); 

Notification notification = new Notification.Builder(this) 
     .setContentIntent(pendingIntent) 
     .setContentTitle("Title") 
     .setSmallIcon(android.R.drawable.ic_menu_more) 
     .build(); 

NotificationManager notificationManager = 
     (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

notificationManager.notify(1, notification); 

ich habe auch showOnLockScreen="true" zum Manifest:

<activity 
    android:name=".MainActivity" 
    android:label="@string/app_name" 
    android:showOnLockScreen="true" 
    android:theme="@style/AppTheme.NoActionBar"> 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 

     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 

Dinge, die arbeiten:

  • Die Aktivität gezeigt wird, wenn das Gerät gesperrt ist (zum Beispiel, wenn ich die Aktivität auf den Vordergrund verlassen und sperren Sie das Telefon die Aktivität bleibt auf den Vordergrund, ohne dass das Telefon entsperren)
  • Wenn ich die Benachrichtigung tippen, wenn das Telefon gesperrt ist, fragt er mich, es zu entsperren und dann wird die Aktivität

ich das gleiche wollen in der Lage gezeigt, zu tun, aber ohne das Gerät zu entriegeln.
Was fehlt mir?

Antwort

-2

Try this !!!

private NotificationCompat.Builder mBuilder; 

Intent notifyIntent = new Intent(getApplicationContext(), MainActivity.class); 
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); 
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notifyIntent, 0); 
    mBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(getNotificationIcon()) 
      .setContent(remoteViews) 
      .setContentIntent(pendingIntent) 
      .setOnlyAlertOnce(true) 
      .setOngoing(true); 

diese Methode Benachrichtigungssymbol erhält auf dem Gerät 5.0 und niedriger

private int getNotificationIcon() { 
    boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP); 
    return useWhiteIcon ? R.drawable.notification_icon : R.mipmap.ic_launcher; 
} 

Remove Code unten in onCreate

Window window = this.getWindow(); 
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); 
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); 
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); 
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
+0

Der 'remoteViews' Variable nicht deklarierte (noch vergeben). – pomber

+0

Ich habe den Rest Ihres Setups ausprobiert und es behebt mein Problem nicht. Sind Sie sicher, dass mit diesem Code eine Aktivität von einer Benachrichtigung auf dem Sperrbildschirm aus gestartet werden kann, ohne das Gerät zu entsperren? – pomber

+0

Ich bin mir sicher. Es funktioniert sehr gut an meiner App! –