رسیور برای دکمه پاور
سلام. لطفااگه کسی درمورد نوشتن رسیور (BroadcastReceiver) برای فشار دادن دکمه پاور میدونه قرار بده. توجه کنید که کدی مثل این فایده ای نداره چون توی سرویس و رسیور کار نمیکنه یعنی کلا تعریف نشده و ارور میده
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
//code
return true;
}
return super.dispatchKeyEvent(event);
}
ممنون
سلام
AndroidManifest.xml:
<receiver android:name=".PowerConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
کلاس مربوط به برودکست:
public class PowerConnectionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// دستور مورد نظر
}
}
اگه متوجه نشدی قسمت آموزش دریافت اس ام اس استاد رو نگاه کن.
سلام
من هم میخوام همین کار رو انجام بدم ولی به مشکل خوردم
کدمو میذارم پایین بهم بگین اشکالم کجاس!!
public class PowerConnectionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
Toast.makeText(MyAppApplication.Activity, "true power", Toast.LENGTH_LONG).show();
} else if (intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)) {
Toast.makeText(MyAppApplication.Activity, "false power", Toast.LENGTH_LONG).show();
}
}
منیفست هم که مثل بالا گذاشتم :
<receiver
android:name=".PowerConnectionReceiver" >
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
ولی کار نمیده انگار!
توی اکتیویتی چطور برودکست رو فعال کنم؟ فکر میکنم ایراد کارم این باشه..
ببینین این کدی هست که من دارم :
public class PowerConnectionReceiver extends BroadcastReceiver {
public static boolean PowerConnection = false;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
PowerConnection = true;
} else if (intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)) {
PowerConnection = false;
}
}
اینم کد من توی متد onResume اکتیویتیم :
@Override
protected void onResume() {
super.onResume();
if (PowerConnectionReceiver.PowerConnection) {
Log.d("onResume ", " PowerConnection " + PowerConnectionReceiver.PowerConnection);
} else if ( !PowerConnectionReceiver.PowerConnection) {
Log.d("onResume ", " PowerConnection " + PowerConnectionReceiver.PowerConnection);
}
}
protected void onPause() {
super.onPause();
if (PowerConnectionReceiver.PowerConnection) {
Log.d("onPause ActivityAnalyze", "PowerConnection " + PowerConnectionReceiver.PowerConnection);
} else if ( !PowerConnectionReceiver.PowerConnection) {
Log.d("onPause ActivityAnalyze", " PowerConnection " + PowerConnectionReceiver.PowerConnection);
}
}
ولی همش false برمیگردونه!
اینو اضافه کن قسمت پریمیشن :
<uses-permission android:name="android.permission.DEVICE_POWER" />
به جای ریسیور بالا اینو به جاش بنویس:
<receiver android:name=".PowerConnectionReceiver" >
<intent-filter>
<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.SCREEN_OFF" >
</action>
<action android:name="android.intent.action.SCREEN_ON" >
</action>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" >
</action>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" >
</action>
<action android:name="android.intent.action.ACTION_SHUTDOWN" >
</action>
</intent-filter>
</receiver>
امتحان کن ببین جواب میده یا نه
فقط یه نکنته اگه از جنی موشن استفاده میکنی برای خاموش کردن جنی موشن باید دکمه ی پاور رو نگه داری تا یه دایالوگ برات باز بشه بعدش خاموشش کنی برای اینکه بهتر بفهمی که کدت داره درست کار میکنه یا نه یه log تویه کلاس
PowerConnectionReceiver
بنویس
با این کد درست شد...
درون اکتیویتی اینارو اضافه میکنیم و دیگه نیازی به هیچ جیزی نیست!
BroadcastReceiver mybroadcast = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
Log.i("[BroadcastReceiver]", "Screen ON " );
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
Log.i("[BroadcastReceiver]", "Screen OFF " );
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.root);
registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_ON));
registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_OFF));
}
/////////////////////////////////////// این رو هم حتما بذارین که خطا نده
@Override
protected void onDestroy() {
try {
if (mybroadcast != null)
unregisterReceiver(mybroadcast);
}
catch (Exception e)
{
}
super.onDestroy();
}
}
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .