2017-08-10 43 views
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在继承

+0

发布您的当前代码,我们将尽力帮助 –

+0

这是一个不错的主意,我会对你已经尝试过的东西感兴趣 – Bqin1

+0

请帮我我怎么做才能识别连续的电源按钮点击。 –

回答

1

Kindly check the link

通过使用广播接收器,您可以获得屏幕关闭的动作,并可以帮助您跟踪动作。

相关问题