broadcastreceiver

How to detect when user turn on/off gps state?

蓝咒 提交于 2019-11-27 14:00:25
I want to block the user from changing WiFi, GPS and loading settings from my application. The user need not on/off WiFi and GPS while running my application.(From notification bar). Is there any BroadcastReceiver exist for listening GPS on/off? Well I did a lot of digging and found that addGpsStatusListener(gpsStatusListener) was deprecated in API 24. And for me, this didn't even work! So, here is another alternative solution to this. If in your app, you want to listen to the GPS state change (I mean On/Off by user) . Using a Broadcast surely is the best approach. Implementation: /** *

ACTION_BATTERY_CHANGED firing like crazy

女生的网名这么多〃 提交于 2019-11-27 13:43:58
Okay, so I'm working on an AppWidget that checks the battery level and displays it on a TextView. My code looks like this: public class BattWidget extends AppWidgetProvider { private RemoteViews views = new RemoteViews("com.nickavv.cleanwidgets", R.layout.battlayout); @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int appWidgetIds[]) { final int N = appWidgetIds.length; context.getApplicationContext().registerReceiver(this,new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); for (int i = 0; i < N; i++) { int appWidgetId = appWidgetIds[i]; appWidgetManager

LocalNotification with AlarmManager and BroadcastReceiver not firing up in Android O (oreo)

心已入冬 提交于 2019-11-27 13:36:26
I've got my local notifications running on androids prior to SDK 26 But in a Android O I've got the following warning, and the broadcast receiver is not fired. W/BroadcastQueue: Background execution not allowed: receiving Intent { act=package.name.action.LOCAL_NOTIFICATION cat=[com.category.LocalNotification] flg=0x14 (has extras) } to package.name/com.category.localnotifications.LocalNotificationReceiver From what I've read broadcast receivers are more restricted in android O, but if so, how should I schedule the broadcast if I want it launching even if the main activity is not running?

When to unregister BroadcastReceiver? In onPause(), onDestroy(), or onStop()?

牧云@^-^@ 提交于 2019-11-27 13:31:03
问题 When should I use unregisterReceiver? In onPause() , onDestroy() , or onStop() ? Note: I need the service to run in the background. Update: I get an exception releasing receivers null . Activity has leaked intent receivers are you missing call to unregisterReceiver(); Please tell me if there's something wrong, here's my code: private boolean processedObstacleReceiverStarted; private boolean mainNotificationReceiverStarted; protected void onResume() { super.onResume(); try { registerReceivers(

Android Alarm Manager with broadcast receiver registered in code rather than manifest

廉价感情. 提交于 2019-11-27 13:18:42
I want to use an alarm to run some code at a certain time. I have successfully implemented an alarm with the broadcast receiver registered in the manifest but the way i understand it, this method uses a separate class for the broadcast receiver. I can use this method to start another activity but I cant use it to run a method in my main activity? ( how can I notify a running activity from a broadcast receiver? ) So I have been trying to register my broadcast receiver in my main activity as explained in the answer above. private BroadcastReceiver receiver = new BroadcastReceiver(){ @Override

Start Activity screen even if screen is locked in Android

前提是你 提交于 2019-11-27 12:54:59
How to start an Activity on device even if screen is locked. I tried as below but it's not working. Broadcast receiver: Intent alarmIntent = new Intent("android.intent.action.MAIN"); alarmIntent.setClass(context, Alarm.class); alarmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); alarmIntent.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED + WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD + WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON + WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); context.startActivity(alarmIntent); You need the following permission in AndroidManifest.xml file:

Activity has leaked IntentReceiver

醉酒当歌 提交于 2019-11-27 12:54:39
问题 I am trying to send sms amd mail together. There are no issues with sending mail, but when i send sms I receive this Exception: End has leaked IntentReceiver Are you missing a call to unregisterReceiver()? Here is my code for sms method: public class End extends Activity { Button btnSendSMS; EditText txtPhoneNo; EditText txtMessage; public EditText Details; public String user; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super

How to set an alarm to fire properly at fixed time?

我只是一个虾纸丫 提交于 2019-11-27 12:34:46
问题 I have this code Calendar c = new GregorianCalendar(); c.add(Calendar.DAY_OF_YEAR, 1); c.set(Calendar.HOUR_OF_DAY, 23); c.set(Calendar.MINUTE, 22); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); // We want the alarm to go off 30 seconds from now. long firstTime = SystemClock.elapsedRealtime(); firstTime += 30*1000; long a=c.getTimeInMillis(); // Schedule the alarm! AlarmManager am = (AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE); am.setRepeating(AlarmManager.ELAPSED

How do I pass data from a BroadcastReceiver through to an Activity being started?

独自空忆成欢 提交于 2019-11-27 12:31:07
I've got an Android application which needs to be woken up sporadically throughout the day. To do this, I'm using the AlarmManager to set up a PendingIntent and have this trigger a BroadcastReceiver. This BroadcastReceiver then starts an Activity to bring the UI to the foreground. All of the above seems to work, in that the Activity launches itself correctly; but I'd like the BroadcastReceiver to notify the Activity that it was started by the alarm (as opposed to being started by the user). To do this I'm trying, from the onReceive() method of the BroadcastReceiver to set a variable in the

Ringer mode change listener Broadcast receiver?

自古美人都是妖i 提交于 2019-11-27 12:29:19
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE); switch (am.getRingerMode()) { case AudioManager.RINGER_MODE_SILENT: Log.i("MyApp","Silent mode"); break; case AudioManager.RINGER_MODE_VIBRATE: Log.i("MyApp","Vibrate mode"); break; case AudioManager.RINGER_MODE_NORMAL: Log.i("MyApp","Normal mode"); break; } From the code above I can get the ringer mode. What I would liek to do is listen the ringer mode changes and call a function. What I have been told is that I can register the AudioManager. RINGER_MODE_CHANGED_ACTION and listen the change intent in broadcastreceiver