I am new to android and trying to develop an application for quick help. As soon as the user double taps power button, I want my application to call the ambulance directly for both cases (Screen on/off). I have written this code but it doesn't work. Any help will be appreciated. Thank you in advance. :)
Android.manifest
<receiver android:name=".MyReceiver" android:exported="true" android:enabled="true">
<intent-filter android:priority="999">
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
<service android:name=".MyCallService" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</service>
MyCallService.java
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
public class MyCallService extends Service {
public MyCallService() {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_USER_PRESENT);
final BroadcastReceiver mReceiver = new MyReceiver();
registerReceiver(mReceiver, filter);
return super.onStartCommand(intent, flags, startId);
}
}
MyReceiver.java
import android.Manifest;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.Toast;
/**
* Created by Kinjal on 2/18/2018.
*/
public class MyReceiver extends BroadcastReceiver {
static int countPowerOff=0;
public static boolean wasScreenOn = true;
private static final int code=1;
@Override
public void onReceive(Context context, Intent intent){
Log.v("onReceive","Power button is pressed");
Toast.makeText(context,"power button clicked",Toast.LENGTH_SHORT).show();
if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
countPowerOff++;
Toast.makeText(context,"Screen off",Toast.LENGTH_SHORT).show();
if(countPowerOff==2){
if(ContextCompat.checkSelfPermission(context,android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) context, new String[] {Manifest.permission.CALL_PHONE},code);
}
else{
context.startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "108")));
}
}
else if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
countPowerOff++;
Toast.makeText(context,"Screen on",Toast.LENGTH_SHORT).show();
if(countPowerOff==2){
if(ContextCompat.checkSelfPermission(context,android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) context, new String[] {Manifest.permission.CALL_PHONE},code);
}
else{
context.startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "108")));
}
}
}
else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)){
Log.e("LOB","userpresent");
Log.e("LOB","wasScreenOn"+wasScreenOn);
}
}
}
}
MainActivity.java
startService(new Intent(getApplicationContext(), MyCallService.class)); //a call to start Service