How to start a new activity with lockscreen?

I create a simple contact management widget that allows the user to type and send SMS to the desired contact.

It works fine as a “normal widget”, but when I add it as a lockscreen widget on Android 4.2, the sms or dial app does not start. In fact, they are star-shaped, but “for” blocked screens, so the user still needs to manually unlock the screen in order to be able to type / send SMS.

I searched the web for some solution, but nothing came in handy. I know FLAG_DISABLE_KEYGUARD or FLAG_SHOW_WHEN_LOCKED, but since sms / dial applications are not “mine”, so I don’t know if they set the correct flag. As a workaround, I tried to create my activity, which sets this flag, and then just launches the desired one (dial or sms), but that does not help.

There is a way to unlock the screen, but this is due to the use of KeyguardManager and KeyguardLock (which work fine), but as a result of using KeyguardLock.newKeyguardLock () I get a message about the impossibility of automatically locking the phone, I do not release this lock (it makes the lock appear again, which I do not want).

In fact, should this widget work similarly to standard SMS widgets or email widgets on the lock screen?

So my question is: how to achieve this and start a new activity with lockscreen?

+6
source share
1 answer

Well, I found the solution myself. It turned out that I was close :)

To start a third-party application / activity, the simplest solution is to create some kind of proxy activity, which will set the appropriate flags in the window, and then start the desired activity and exit.

An example code is shown below:

call intent to widget (calling proxy):

@Override public void onReceive(Context context, Intent intent) { Utilities.printLog(TAG, "onReceive"); Utilities.printLog(TAG, "intent: " + intent); if (intent.getAction().equals(ACTION)) { final String number = intent.getStringExtra(EXTRAS); Toast.makeText(context, "Selected number: " + number, Toast.LENGTH_SHORT) .show(); /** REMOVING KEYGUARD RECEIVER **/ // not really an option - lock is still holded by widget and screen // cannot be locked again ;( // KeyguardManager keyguardManager = (KeyguardManager) context // .getSystemService(Context.KEYGUARD_SERVICE); // KeyguardLock lock = keyguardManager // .newKeyguardLock(Context.KEYGUARD_SERVICE); // lock.disableKeyguard(); final Intent activity = new Intent(context, MainActivity.class); activity.putExtras(intent.getExtras()); activity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); activity.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); activity.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); context.startActivity(activity); } super.onReceive(context, intent); } 

in proxy activity just call:

  @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().requestFeature(Window.FEATURE_NO_TITLE); // getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); final Intent callingIntent = getIntent(); final String actionToLaunch = callingIntent.getStringExtra(ContactsStackWidgetProvider.ACTION); final String number = callingIntent.getStringExtra(ContactsStackWidgetProvider.EXTRAS); final Intent activity = new Intent(); if (actionToLaunch.equals(Intent.ACTION_DIAL)) { activity.setAction(Intent.ACTION_DIAL); activity.setData(Uri.parse("tel:"+number)); } else if (actionToLaunch.equals(Intent.ACTION_SENDTO)) { activity.setAction(Intent.ACTION_SENDTO); activity.setData(Uri.parse("sms:"+number)); } else { throw new IllegalArgumentException("Unrecognized action: " + actionToLaunch); } new Handler().postDelayed(new Runnable() { @Override public void run() { startActivity(activity); finish();//it is important to finish, but after a small delay } }, 50L); } 
+7
source

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


All Articles