2
我想在按兩次電源按鈕時發送一封電子郵件?我已經嘗試了很多代碼,但目前還沒有任何代碼正在工作。任何人都可以幫忙嗎?如何在按下電源按鈕時執行我的應用程序的某些功能?
這是我服務類
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在繼承
發佈您的當前代碼,我們將盡力幫助 –
這是一個不錯的主意,我會對你已經嘗試過的東西感興趣 – Bqin1
請幫我我怎麼做才能識別連續的電源按鈕點擊。 –