gpt4 book ai didi

android - 按下电源按钮时如何执行我的应用程序的某些功能?

转载 作者:行者123 更新时间:2023-11-29 01:08:00 25 4
gpt4 key购买 nike

我想在按下电源按钮两次时发送电子邮件?我已经尝试了很多代码,但到目前为止没有一个有效。有人可以帮忙吗?

这是我的服务等级

public class UpdateService extends Service {

BroadcastReceiver mReceiver;
int counter=0;

@Override
public void onCreate() {
super.onCreate();
// register receiver that handles screen on and screen off logic
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
mReceiver = new Receiver();
registerReceiver(mReceiver, filter);
}

@Override
public void onDestroy() {

unregisterReceiver(mReceiver);
Log.i("onDestroy Reciever", "Called");

super.onDestroy();
}

@Override
public void onStart(Intent intent, int startId) {
boolean screenOn = intent.getBooleanExtra("screen_state", false);
if (!screenOn) {
counter += 1;
Log.i("screenON *****************", "Called");
Toast.makeText(getApplicationContext(), "Awake" + counter, Toast.LENGTH_LONG)
.show();
} else {
counter += 1;
Log.i("screenOFF ******************", "Called");
Toast.makeText(this, "slept" + counter, Toast.LENGTH_SHORT).show();
}
if (counter >= 4) {
Log.e("counter is -->", "" + counter);
counter = 0;
Log.e("counter is after clearance -->", "" + counter);
Log.e("************-***********", "Boooyah");
Toast.makeText(this, "Booyaaaha !!!!!!!!!!!!!!!!", Toast.LENGTH_SHORT).show();


Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{"dracmore@outlook.com"});
email.putExtra(Intent.EXTRA_SUBJECT, "TFS");
email.putExtra(Intent.EXTRA_TEXT, "This is the sample Message of my email");
email.setType("email/rfc822");
startActivity(Intent.createChooser(email, "Send Mail Via"));
email.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
email.addFlags(Intent.FLAG_FROM_BACKGROUND);
startActivity(email);
}
}

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}

这个小家伙是接收器

public class Receiver extends BroadcastReceiver {

private boolean screenOff;

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOff = false;
}
Intent i = new Intent(context, UpdateService.class);
i.putExtra("screen_state", screenOff);
context.startService(i);
}

}

list

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.apkglobal.transarent">

<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.MANAGE_OWN_CALLS"></uses-permission>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:name=".Receiver"/>
<service android:name=".UpdateService"/>
</application>

</manifest>

问题是代码在屏幕关闭和打开时工作正常但我希望此代码在连续按下电源按钮 2 次或 3 次时工作

最佳答案

Kindly check the link

通过使用广播接收器,您可以获得屏幕关闭的 Action ,借助它您可以跟踪 Action 。

关于android - 按下电源按钮时如何执行我的应用程序的某些功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45603754/

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