【发布时间】: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? -
设备默认调用。我希望我的应用程序在双击电源按钮时屏幕打开或关闭时呼叫救护车。
-
好的。我不太了解如何在锁定模式下获得电源按钮操作,也许您需要为此获得系统级权限。也许一些专家会建议你。继续挖掘。