【问题标题】:Make a call on double tap of power button双击电源键拨打电话
【发布时间】:2018-02-18 06:07:26
【问题描述】:

我是 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

【问题讨论】:

  • 关注This answer 并检查是否有双击。
  • @ADM 这没用。我可以调用 onReceive 方法并获得这些祝酒词。我打电话有问题。无论如何,谢谢。
  • 什么电话?设备默认调用?或VOIP?
  • 设备默认调用。我希望我的应用程序在双击电源按钮时屏幕打开或关闭时呼叫救护车。
  • 好的。我不太了解如何在锁定模式下获得电源按钮操作,也许您需要为此获得系统级权限。也许一些专家会建议你。继续挖掘。

标签: android broadcastreceiver


【解决方案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

【讨论】:

    【解决方案2】:

    或者你可以试试这个

        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);
    }
    

    【讨论】:

      猜你喜欢
      • 2011-07-21
      • 2016-05-07
      • 1970-01-01
      • 2017-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多