StartActivity for URI in Lockscreen on Android Nougat

I use BroadcastReceiverone that gets broadcast from AlarmManager. In the receiver, I start two classes. One action is launched from a URI like this and is a third-party application:

// Open spotify
Intent spotify = new Intent(Intent.ACTION_VIEW, Uri.parse(song));
spotify.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

try {
    context.startActivity(spotify);
} catch (ActivityNotFoundException e) {
    status = Status.SPOTIFY_NOT_INSTALLED;
}

After that, I launch another action that belongs to the application with a 5 second delay with AlarmManager:

public static void setExact(
        Context context, PendingIntent pendingIntent, long time
) {

    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        am.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent);
    else
        am.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);

}

public static void setExactDelay(
        Context context, PendingIntent pendingIntent, long delay
) {
    setExact(context, pendingIntent, System.currentTimeMillis() + delay);
}

    PendingIntent pendingIntent = AlarmPlayActivity.makePendingIntent(context, alarm, status, startTime);
    AlarmSet.setExactDelay(context, pendingIntent, 5000);

The second action begins after 5 seconds, as expected. However, the first action only starts when the device is unlocked. If the device is locked, it does not start on Android Nougat (7.0). This happens even when the lock is not protected by a password, pattern, etc. This was used to work with earlier versions of Android, even with a secure lock.

, ?

: IntentService. , , , :

public class AlarmService extends IntentService {

    static final int NOTIFICATION_ID = 1;

    public AlarmService() {
        super("AlarmService");
    }

    public static Intent makeIntent(Context context, Alarm alarm, AlarmReceiver.Status status, long startTime) {

        Intent intent = IntentFactory.alarmPlayIntent(alarm, status, startTime);
        intent.setClass(context, AlarmService.class);

        return intent;

    }

    private static void sleep(long time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            // Nothing
        }
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        // Acquire Wakelock immediately

        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

        PowerManager.WakeLock wakeLock = pm.newWakeLock(
                PowerManager.FULL_WAKE_LOCK |
                PowerManager.ACQUIRE_CAUSES_WAKEUP |
                PowerManager.ON_AFTER_RELEASE,
                "AlarmServiceWakeLock"
        );

        wakeLock.acquire();

        KeyguardManager.KeyguardLock lock = ((KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE)).newKeyguardLock(KEYGUARD_SERVICE);
        lock.disableKeyguard();

        // Get intent data

        final Alarm alarm = IntentFactory.getAlarm(intent);
        AlarmReceiver.Status status = IntentFactory.getStatus(intent);
        final long startTime = IntentFactory.getStartTime(intent, 0);

        // Get a random song for this alarm

        AlarmDatabase db = AlarmDatabase.getInstance(this);
        Song song = db.getRandomSong(alarm);

        String songName = song == null ? "backup sound" : song.getName();

        // Start a foreground notification

        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle(getText(R.string.alarm_starting_notification_title))
                .setContentText(getString(
                        R.string.alarm_starting_notification_message, alarm.getName(), songName
                ))
                .setSmallIcon(R.drawable.ic_launcher)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .build();

        startForeground(NOTIFICATION_ID, notification);

        // Potentially open Spotify if we can

        if (song != null) {

            // Open spotify

            Intent spotify = new Intent(Intent.ACTION_VIEW, Uri.parse(song.getUri()));
            spotify.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

            try {
                startActivity(spotify);
            } catch (ActivityNotFoundException e) {
                status = AlarmReceiver.Status.SPOTIFY_NOT_INSTALLED;
            }

        } else
            status = AlarmReceiver.Status.NO_SONGS;

        // Start play activity in 10 seconds, giving Spotify some chance to load up.

        sleep(10);
        startActivity(AlarmPlayActivity.makeIntent(this, alarm, status, startTime));

        // Keep alive for 5 more seconds
        sleep(5);

        // Stop notification
        stopForeground(true);

        // Release wakelock
        wakeLock.release();


    }

}
+4
4

, , Intent.ACTION_VIEW , - .

.

+2

: am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, nexttime, pendingintent);

am.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, nexttime, pendingintent);

: :

  PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
            wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
                    PowerManager.ACQUIRE_CAUSES_WAKEUP |
                    PowerManager.ON_AFTER_RELEASE, A_.APPNAME + Integer.toString(index));
        if (wakeLock != null && wakeLock.isHeld()){
            wakeLock.acquire();
        }

:

wakeLock.release();

( ) : fooobar.com/questions/807623/...

import android.view.Window;
import android.view.WindowManager.LayoutParams;


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

, :

fooobar.com/questions/94037/...

 @Override
    public void onReceive(Context context, Intent intent) {
        //start activity
        Intent i = new Intent();
        i.setClassName("com.test", "com.test.MainActivity");
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
+1

, Spotify , Service .

wakelock Foreground ( notification NotificationCompat.Builder, startForeground(mId, notification);, , Nougat .

Spotify Intent , , , , wakelock. , , . , !

+1

Source: https://habr.com/ru/post/1663786/


All Articles