Waking up device do not work
Asked Answered
U

2

0

I used this code to wake up my device (Moto G 1 gen) when screen is off, but it seems that it doesn't work. It works only when screen is on.

edit: Now it works, but CallScreen.class shows for 1 seconds and then finishes. LogCat provides no information for that.

Code:

Intent intent = new Intent(MainActivity.this, NiceBroadcastReceiver.class);
    intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
    intent.putExtra("name",name);
    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + timeToCall, pendingIntent);

NiceBroadcastReceiver extends BroadcastReceiver:

 @Override
public void onReceive(Context context, Intent intent) {
    String name = intent.getExtras().getString("name");

    Intent i = new Intent(context, CallScreen.class);
    i.setClassName("(package)", "(class)");
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.putExtra("name2", name);
    context.startActivity(i);
    //Toast.makeText(context, name, Toast.LENGTH_SHORT).show();
}

CallScreen.class:

PowerManager.WakeLock fullWakeLock;
    PowerManager.WakeLock partialWakeLock;

    PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    fullWakeLock = powerManager.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "Loneworker - FULL WAKE LOCK");
    partialWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Loneworker - PARTIAL WAKE LOCK");

    if(fullWakeLock.isHeld()){
        fullWakeLock.release();
    }
    if(partialWakeLock.isHeld()){
        partialWakeLock.release();
    }

    fullWakeLock.acquire();

    KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
    KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("TAG");
    keyguardLock.disableKeyguard();

    RelativeLayout rl = (RelativeLayout) findViewById(R.id.relLay);
    rl.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           finish();
        }
    });

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        name = extras.getString("name2");
    }

    incomingCall = (TextView) findViewById(R.id.textIncomingView);
    caller = (TextView) findViewById(R.id.callerId);
    caller.setText(name);

    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
    mp = MediaPlayer.create(getApplicationContext(), notification);
    mp.start();
Undecided answered 12/4, 2015 at 15:35 Comment(7)
"It works only when screen is on" -- how are you determining this? Bear in mind that, from your code, it appears that you are attempting to send a broadcast to Android's BroadcastReceiver class, and the idea is that you're supposed to make a subclass of that.Gent
Well, the BroadcastReceiver's subclass is of course not "BroadcastReceiver". Just changed it here to make it more clear.Goldsberry
OK. That does not answer how you are determining that "it works only when screen is on"? For example, if the class that is not named BroadcastReceiver is starting a service, unless you are using WakefulBroadcastReceiver or my WakefulIntentService, the device may fall back asleep before the work gets done.Gent
When I push power button to get screen asleep then it doesn't work. What I noticed that after time that it should fire when I unlock device I can see a glimpse of called activity so it fires, but doesn't unlock device and appear on screen.Goldsberry
You do not appear to have any code to "unlock device and appear on screen".Gent
I thought it's this: AlarmManager.ELAPSED_REALTIME_WAKEUPGoldsberry
No one else? I'm still fighting my code...Goldsberry
U
1

Okay, I got it. Seems to me that FLAGS don't do their job properly so the beef is after setContentView:

PowerManager.WakeLock wl
...
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
                    WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN |
                    WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_call_screen);

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "My Tag");
    wl.acquire();

    KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
    KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("TAG");
    keyguardLock.disableKeyguard();
Undecided answered 13/4, 2015 at 13:48 Comment(0)
G
1

ELAPSED_REALTIME_WAKEUP will only power on the CPU, and that only until onReceive() of your BroadcastReceiver returns.

So:

  1. The CPU may fall asleep again before your activity appears, as startActivity() is asynchronous, and onReceive() will return before startActivity() really begins its processing

  2. ELAPSED_REALTIME_WAKEUP does not turn on the screen, which you appear to want

  3. ELAPSED_REALTIME_WAKEUP does not unlock the device, which you appear to want

Gent answered 12/4, 2015 at 17:8 Comment(4)
Okay, I search for unlocking code schemes and now I see that I'm missing quite a chunk of a code. I'm now thinking if would it be better to put before intent in BroadcastReceiver's "onReceive" method so the screen is waken up and unlocked AND then activity is started and after that release wakeLock.Goldsberry
@jeand'arme: I haven't attempted to turn on the screen and unlock it, so I don't know what the appropriate process and timing are for that -- sorry!Gent
I will experiment and will let here know what I will get :)Goldsberry
I updated my code. I put WakeLock features in target class and it works properly, but for like 1 second. Screen wakes up, unlock, You can hear sound for like second and then it finishes.Goldsberry
U
1

Okay, I got it. Seems to me that FLAGS don't do their job properly so the beef is after setContentView:

PowerManager.WakeLock wl
...
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
                    WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN |
                    WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_call_screen);

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "My Tag");
    wl.acquire();

    KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
    KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("TAG");
    keyguardLock.disableKeyguard();
Undecided answered 13/4, 2015 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.