19

I've an app that starts itself if the phone is booted. A user told me his phone is used by two people, one of them is using my app and one not.

So I need some event to listen to when the user is switched, so that I can start my apps service if the correct user is using the phone. Anything I can use for that?

Edit

I'm listening to the boot event with a broadcast receiver registered in the manifest, so I know what this is. But I could not find anything suitable for switching users on a device

prom85
  • 16,896
  • 17
  • 122
  • 242
  • Can you explain it little more? As per my understanding till now you want a multiuser device like Windows desktop system, where the system can do with a multiuser system. If I am right then you need to create a home launcher app to choose the user profile, this app will launch on the reboot of the device and comes to the front of the user. However, the user selects its user type then you can do the action according to it. – Ready Android Jan 29 '18 at 05:30
  • 3
    Multi user support is provided by android itself on some devices. It's selected by some system provided mechanism and you don't need to implement anything for this. Android will run apps in user specific environments. My app is an always running service for one of those users, so it must be started if the current user is changed to a user, that has my app installed. Therefore I need an event to catch this and to start my apps service... – prom85 Jan 29 '18 at 08:07
  • 1
    I suggest that you either add [Google sign-in](https://developers.google.com/identity/sign-in/android/) to your app, or [support multiple users](https://source.android.com/devices/tech/admin/multi-user). – RonTLV Jan 29 '18 at 14:11
  • 1
    In case if you haven't seen this post, https://stackoverflow.com/questions/15392126/how-to-detect-switching-between-users – LincyTonita Jan 30 '18 at 13:32
  • Get the [ANDROID_ID](https://developer.android.com/reference/android/provider/Settings.Secure.html#ANDROID_ID), with that you can know which user is yours. – sharp Feb 01 '18 at 07:14

1 Answers1

-5

You need to look for something called BroadcastReciever in android. They are used to capture events such as camera click, phone booting up, screen unlocked etc... These events have a callback called onReceive where you can implement your login.

It's quite easy and you can Google it.

In your manifest:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

In your application element (be sure to use a fully-qualified [or relative] class name for your BroadcastReceiver):

<receiver android:name="com.example.MyBroadcastReceiver">  
    <intent-filter>  
        <action android:name="android.intent.action.BOOT_COMPLETED" />  
    </intent-filter>  
</receiver>

In MyBroadcastReceiver.java:

package com.example;

public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startServiceIntent = new Intent(context, MyService.class);
        context.startService(startServiceIntent);
    }
}
Ayusch
  • 417
  • 7
  • 17
  • 2
    "I've an app that starts itself if the phone is booted" so I know this, but I can't find an intent to listen to for user changes... – prom85 Jan 20 '18 at 20:42