Start service when device is unlocked - android

I want to start service only when device is unlocked. I've tried this code but it didn't work.Can somebody explain me what do I do wrong?And what should I do?
if( myKM.isKeyguardLocked()==false) {
startService(intent);
Log.i("Point ","5");
} else {
Log.i("Point ","6");
stopService(intent);
}

You can have a Broadcast Receiver for ACTION_USER_PRESENT
No you can check in onReceive (Context context, Intent intent) as below -
if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)){
// device unLocked
}

Related

How to Allow MIUI "start in Background" permission for my application

i want "start in Background" permission for open incoming and outgoing call screen when app close.
public class CallReciver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("onReceive","=========>>>>");
//start activity
Intent i = new Intent(context.getApplicationContext(), OngoingCallActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addCategory(Intent.CATEGORY_LAUNCHER);
context.startActivity(i);
}
}
receiver is opening incoming or outgoing UI screen.but receiver is able to open activity when app in foreground. onReceive method call but receiver not able to open activity when app in background, i try lot's of solution for that but nothing to work.
when manually allow start in background permission it work fine.
so,how to allow start in background permission in MIUI software.Please help me i spend lot's of day for that.sorry for bad english and thanks in advance.
For MI device follow below steps
Intent localIntent = new Intent("miui.intent.action.APP_PERM_EDITOR");
localIntent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
localIntent.putExtra("extra_pkgname", getPackageName());
startActivity(localIntent);

I am new to android. I wish to use android service for blocking call only when the service is started

This is the code should be run only when the service is running. And the service should be stopped in some other part.
public void onReceive(Context context, Intent intent)
{
this.context=context;
this.intent=intent;
if (!intent.getAction().equals("android.intent.action.PHONE_STATE"))
return;
else
{
number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (Callblockactivity.blockList.contains(new Blacklist(number))) {
System.out.println("i");
}
else {
disconnectPhoneItelephony(context);
return;
}
}
}
Now, the service should be started in mainactivity as follow.
if (speed >10) {
//service should be started.
}
else {
//service should be stopped
}
Please help with this.
use startService to start a Service. Use stopSelf or stopService to stop your service.
And you may use a static boolean variable inside your service in order to keep the flag whether the service is running.
Instead of that variable also you may dynamically register a receiver when you start your service, and unregister it when you stop the service.

Broadcast Receiver in a service but intent not getting caught on service once process is killed

I have a simple widget that does some calculations once the screen comes on and displays them and clears all the fields once the screen goes off ... i have a broadcast receiver setup in my service which listens to ACTION_SCREEN_ON and ACTION_SCREEN_OFF.
This works perfectly as long as the phone doesn't go to sleep for a long period of time or there is heavy usage of the phone - once this happens my widget process is killed (the service is still running but the process is killed) after this when the screen goes off and comes back on my widget doesn't update as the ACTION_SCREEN_ON intent is not caught by my service :(
public class CDTservice extends Service {
#Override
public void onCreate() {
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
m_receiver = new ScreenBroadcastReceiver();
registerReceiver(m_receiver, filter);
Log.d("Widgettool", "works");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
start();
Toast.makeText(getBaseContext(), "SERVICE ON", Toast.LENGTH_SHORT).show();
return START_REDELIVER_INTENT;
}
#Override
public void onDestroy() {
stop();
unregisterReceiver(m_receiver);
}
public void start()
{
RemoteViews View = new RemoteViews(this.getPackageName(), R.layout.main);
updatewidgetclass x= new updatewidgetclass(this, View, widgetId);
x.start(); // does calculations and displays on widget
}
public void stop()
{
RemoteViews Viewclear = new RemoteViews(this.getPackageName(), R.layout.main);
updatewidgetclass y = new updatewidgetclass(this, Viewclear, widgetId);
y.stop(); // clears resources and stops
}
private class ScreenBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.d("ON SCREEN ON", "might hang here");
start();
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
stop();
}
}
}
sometimes when the widget process is not claimed by the android system the widget works perfectly for days and perfectly displays the values ACTION_SCREEN_ON
the problem arises when - i check in settings>apps>running - i can see my widget name and it says 0 processes and 1 service
i assume the broadcast receive is happening on the main process and hence its not receiving it when the process gets killed.
I have a work around in place for this but would really like to fix the issue.
Any helps is highly appreciated
I agree with #CommonsWare that having a service running in the bg at all times just to detect when the screen turns on and off is a very bad idea. Do you really need ACTION_SCREEN_ON, or will ACTION_USER_PRESENT (phone unlocked) suffice? This way, you do not need a service at all, and you can just define the receiver in the manifest.
If you really wanted to, you could register your service/ACTION_SCREEN_OFFreceiver in your ACTION_USER_PRESENT receiver so that the service is only running when the user is actually using the device.
I know this doesn't really answer your question, but it does provide a useful workaround to your problem.

BroadcastReceiver does not receive Intent.ACTION_BATTERY_LOW

I wrote an app, which shows a dialog when charge lvl is 15% or low.
Problem is that on one forum I received the message that it does not work on Google Nexus S (I9023). MIUI 2.3.7c.
code for receiver is (it is registered in manifest):
if (intent.getAction().equals(Intent.ACTION_BATTERY_LOW))
{
try
{
context.startService(myIntent);
}
catch(Exception ex){Toast.makeText(context, "Problem with Starting a Service", Toast.LENGTH_LONG).show();}
}
In service I register receiver with filter Intent.ACTION_BATTERY_CHANGED.
code for service in is:
...
if (batteryLevel == mLowLevelAlarm)
{
if (prefView.getBoolean(PREF_LOW_CHARGE_ON, true))
{
myIntent = new Intent(MyService.this, BatteryLow.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(myIntent);
}
}
...
Can anyone help to solve this problem?
thx

How to know whether the phone is locked mode?

Is there a way for me to know whether the phone is in locked state?
Have your app listen our for the ACTION_SCREEN_OFF broadcast. More information here.
public class ScreenReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
//screen locked
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
//screen unlocked
}
}
}
You might also want to receive information of when the user gets past the keyguard by registering for the ACTION_USER_PRESENT broadcast.

Categories

Resources