Note that there are some explanatory texts on larger screens.

plurals
  1. POchanging UI elements if app is in the foreground from a broadcast receiver
    text
    copied!<p>I Have a <code>BroadcastReceiver</code> set up to turn Bluetooth on and off according to power state (when plugged in, bluetooth is on, unplugged, bluetooth is off). This is working just fine (yay!). however, my very simple app has a single button, which also turns Bluetooth on and off, and has the text "Bluetooth on" or "Bluetooth Off", as applicable. I would like to update this single button, BUT, I only have to update it if the app is in the foreground. Inside onResume on m,y main activity, I'm calling my updateUI method, which checks the Bluetooth state, and updates the button accordingly. however, that only applies if the program was open and in the background, and is resumed, NOT if i'm in the program while plugging/unplugging the power.</p> <p>I created a new activity (<code>CheckIfAppIsRunningActivity.java</code>) with this code which is supposed to check if my app is running in the foreground, and if so, take it to the activity (<code>BluetoothOnActivity</code>) which will update the button:</p> <pre><code> package vermel.BluetoothOn; import java.util.List; import android.app.Activity; import android.app.ActivityManager; import android.app.ActivityManager.RunningAppProcessInfo; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class CheckIfAppIsRunningActivity extends Activity{ public void onCreate() { checkStatus(); } private BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Intent it = new Intent(); it.setClassName("vermel.BluetoothOn", "vermel.BluetoothOn.BluetoothOnActivity"); context.startActivity(it); } }; public void checkStatus() { ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE); List&lt;RunningAppProcessInfo&gt; runningProcInfo = activityManager.getRunningAppProcesses(); for(int i = 0; i &lt; runningProcInfo.size(); i++){ if(runningProcInfo.get(i).processName.equals("vermel.BluetoothOn")) { if (runningProcInfo.get(i).lru == RunningAppProcessInfo.IMPORTANCE_FOREGROUND){ //start activity /* Intent it = new Intent(); it.setClassName("vermel.BluetoothOn", "vermel.BluetoothOn.BluetoothOnActivity"); context.startActivity(it); */ } } } } } </code></pre> <p>and i'm pointing to it from my broadcast receiver:</p> <pre><code> package vermel.BluetoothOn; import android.bluetooth.BluetoothAdapter; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Button; import android.widget.Toast; public class BTDetector extends BroadcastReceiver { BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); public void onReceive(Context context , Intent intent) { String action = intent.getAction(); if (action.equals(Intent.ACTION_POWER_CONNECTED)) { if (!mBluetoothAdapter.isEnabled()) { mBluetoothAdapter.enable(); //TODO if app is open, change text on button to on //Toast.makeText(context, "turned on bluetooth", Toast.LENGTH_LONG).show(); Intent i = new Intent(); i.setClassName("vermel.BluetoothOn", "vermel.BluetoothOn.CheckIfAppIsRunningActivity"); context.startActivity(i); } } else if (action.equals(Intent.ACTION_POWER_DISCONNECTED)) { if (mBluetoothAdapter.isEnabled()){ mBluetoothAdapter.disable(); //TODO if app is open, change text on button to off //Toast.makeText(context, "turned off bluetooth", Toast.LENGTH_LONG).show(); Intent i = new Intent(); i.setClassName("vermel.BluetoothOn", "vermel.BluetoothOn.CheckIfAppIsRunningActivity"); context.startActivity(i); } } } } </code></pre> <p>a few things: yes, I know i'm not supposed to use .enable() without user permission. in a weird way, plugging in the phone IS my user permission, since this is ALL that this app does, so, it's not sneaky, since you know what you're getting when you're installing the app.</p> <p>The commented stuff is mostly things i've tried in vain..</p> <p>I'm very open to the fact that i'm making this WAY harder than I need to...</p> <p>so, as i said, it does turn Bluetooth on and off beautifully, but simply crashes after that. I can't debug it, since the emulator doesn't have Bluetooth , and i'm disconnecting the phone to get the crash result, so, it's not logging anything, since it's now connected...</p> <p>I'm new in both Java and Android, and would appreciate a bit of patience. I try reading the official android documentation, but that's like chinese to me... so, an extended explanation would be great...</p> <p>Thanks for reading!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload