gpt4 book ai didi

android - 双击电源键调用电话

转载 作者:行者123 更新时间:2023-11-30 00:06:41 25 4
gpt4 key购买 nike

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

最佳答案

或者你可以试试这个

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

关于android - 双击电源键调用电话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48848971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com