双击电源按钮拨打电话

时间:2018-02-18 06:07:27

标签: android broadcastreceiver

我是android的新手,并尝试开发一个快速帮助的应用程序。一旦用户双击电源按钮,我希望我的应用程序直接呼叫救护车两种情况(屏幕开/关)。我写了这段代码,但它没有用。任何帮助将不胜感激。先感谢您。 :)

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

2 个答案:

答案 0 :(得分:1)

您必须检测屏幕关闭并且开启 计算它们之间的时差,如果小于4秒(在我的情况下)拨打电话,否则不要

@Override
public void onReceive(final Context context, final Intent intent) {
 cntx = context;
 Log.v("onReceive", "Power button is pressed.");
 if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
  a = System.currentTimeMillis();
  seconds_screenoff = a;
  OLD_TIME = seconds_screenoff;
  OFF_SCREEN = true;

  new CountDownTimer(5000, 200) {

   public void onTick(long millisUntilFinished) {


    if (ON_SCREEN) {
     if (seconds_screenon != 0 && seconds_screenoff != 0) {

      actual_diff = cal_diff(seconds_screenon, seconds_screenoff);
      if (actual_diff <= 4000) {
       sent_msg = true;
       if (sent_msg) {

        Toast.makeText(cntx, "POWER BUTTON CLICKED 2 TIMES", Toast.LENGTH_LONG).show();

       // Make Call
       Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "Your Phone_number"));
       startActivity(intent);

        seconds_screenon = 0 L;
        seconds_screenoff = 0 L;
        sent_msg = false;

       }
      } else {
       seconds_screenon = 0 L;
       seconds_screenoff = 0 L;

      }
     }
    }
   }

   public void onFinish() {

    seconds_screenoff = 0 L;
   }
  }.start();



 } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
  a = System.currentTimeMillis();
  seconds_screenon = a;
  OLD_TIME = seconds_screenoff;

  new CountDownTimer(5000, 200) {

   public void onTick(long millisUntilFinished) {
    if (OFF_SCREEN) {
     if (seconds_screenon != 0 && seconds_screenoff != 0) {
      actual_diff = cal_diff(seconds_screenon, seconds_screenoff);
      if (actual_diff <= 4000) {
       sent_msg = true;
       if (sent_msg) {

        Toast.makeText(cntx, "POWER BUTTON CLICKED 2 TIMES", Toast.LENGTH_LONG).show();
        vibe.vibrate(100);
        seconds_screenon = 0 L;
        seconds_screenoff = 0 L;
        sent_msg = false;



       }
      } else {
       seconds_screenon = 0 L;
       seconds_screenoff = 0 L;

      }
     }
    }

   }

   public void onFinish() {

    seconds_screenon = 0 L;
   }
  }.start();



 }
}

private long cal_diff(long seconds_screenon2, long seconds_screenoff2) {
 if (seconds_screenon2 >= seconds_screenoff2) {
  diffrence = (seconds_screenon2) - (seconds_screenoff2);
  seconds_screenon2 = 0;
  seconds_screenoff2 = 0;
 } else {
  diffrence = (seconds_screenoff2) - (seconds_screenon2);
  seconds_screenon2 = 0;
  seconds_screenoff2 = 0;
 }

 return diffrence;
}

}

查看此帖子:Click here

答案 1 :(得分:0)

或者你可以试试这个

    static int i=0;
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
       i++;
        if(i==2){
    //do something
    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "Your Phone_number"));
    startActivity(intent);
    i = 0;
        }

    }
    return super.onKeyDown(keyCode, event);
}
相关问题