我想在按下电源按钮两次时发送电子邮件吗?我已经尝试了很多代码,但到目前为止都没有工作。有人可以帮忙吗?
这是我的服务班
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[]{"[email protected]"});
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);
}
}
表现
<?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
通过使用广播接收器,您可以关闭屏幕的操作,并借助它可以跟踪操作。
关于android - 按下电源按钮时如何执行我的应用程序的某些功能?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45603754/