I have a thread that listens for an inputStream and can send some data with some ouputStream that I got from a Bluetooth socket.
Here is what I'm trying to achieve: I have a MainActvity And Activity B. I need to have both to be capable to receive data from the Thread or send data to the Thread.
I can't just make a new instance of my thread cause it's going to cut the connection.
I'm a new to android and programming so it's very hard to know what I really need.
I tried my best with handlers, Broadcast receiver but had no luck for making it work in Activity B.
Whatever may work for my case. It would be nice having an example of it.
You can use a LocalBroadcastManager in a Service which runs your Thread and let the Activities register receivers on it.
In your Activity:
final LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getApplicationContext());
final IntentFilter filter = new IntentFilter();
filter.addAction("SAMPLE_ACTION");
lbm.registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive() called with: context = [" + context + "], intent = [" + intent + "]");
}
}, filter);
In your Service
final LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getApplicationContext());
final Intent intent = new Intent("SAMPLE_ACTION");
lbm.sendBroadcast(intent);
Related
I have two apps each with an accessibility service. The accessibility service of one app sends a broadcast and the accessibility service of the other app receives it.
Here is the service sending the broadcast:
Intent intent = new Intent();
intent.setAction("com.corps.mypackage");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //this is needed if broadcast not being sent from activity
sendBroadcast(intent);
And here is the service that receives the broadcast:
IntentFilter intentFilter = new IntentFilter("com.corps.mypackage");
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//Do some things
}
};
registerReceiver(receiver, intentFilter);
The broadcast is not being received. What could be the reason?
As per the Android documentation:
Context-registered receivers receive broadcasts as long as their registering context is valid. For example, if you register within an Activity context, you receive broadcasts as long as the activity is not destroyed. If you register with the Application context, you receive broadcasts as long as the app is running.
Your receiving app's context is likely not valid. Consider using a manifest-declared receiver instead.
I would like to call a non-static method in the for-loop inside the onHandleWork. How can I achieve that?
#Override
protected void onHandleWork(#NonNull Intent intent) {
Log.d(TAG, "onHandleWork");
String input = intent.getStringExtra("inputExtra");
for (int i = 0; i < 10; i++) {
Log.d(TAG, input + " - " + i);
new MainActivity.method();
if (isStopped()) return;
SystemClock.sleep(1000);
}
}
You should broadcast a message that the activity is listening instead.
To achieve that, use a broadcastreceiver on the activity and send It from the job service with context.sendBroadcast(intent)
You can make your activity listening by declaring a broadcastreceiver programatically or in the AndroidManifest.xml file. Note that If you declare it programatically, the activity Will only receive the broadcast If It is opened. If you use the manifest approach, when you activity is not opened and you send a broadcast, the sustém os going to start your activity
Receiving Broadcasts:
https://developer.android.com/guide/components/broadcasts.html#manifest-declared-receivers
Sending Broadcasts:
https://developer.android.com/guide/components/broadcasts.html#sending-broadcasts
Docs:
https://developer.android.com/reference/android/content/BroadcastReceiver
I have a java Class that extends Plugin (PhoneGap), but when inside this class, i call another class that extends Activity, it just doesn't work !. i mean, it seems like it doesn't get called. To confirm this, i have change my second class, this time, not extending from Activity and it works fine. i need teh second one to extends from Activity because i am using this two utilities (getFileStreamPath and openFileOutput) to create a file
File filepath = getFileStreamPath("filename.CPCL"); and openFileOutput
FileOutputStream os = this.openFileOutput(fileName, Context.MODE_PRIVATE);
I have an app with a class which extends a custom Service that calls another class which extends Activity.
First I instantiate the Activity. In the onCreate of your Plugin class use:
// get a handle on your Application
Application app = getApplication();
Intent intent = new Intent(getApplicationContext(), YourActivity.class);
app.startActivity(intent);
This will start your Activity and call the standard Lifecycle events.
The way I handle continued communication with the running Activity is by using a Handler to send a broadcast from your plugin which the Activity picks up in its receiver. In the onCreate of your plugin:
mHandler.post(new Runnable() {
#Override
public void run() {
Log.d(TAG, "Call the Activity");
Intent intent = new Intent(YourActivity.CALL_FROM_PLUGIN);
intent.putExtra("request", <<Any extras you might want to send through>>);
sendBroadcast(intent);
}
});
In the Activity I declare the variable:
public static final String CALL_FROM_PLUGIN= "CALL_FROM_PLUGIN";
then in onCreate() I added the following;
IntentFilter filter = new IntentFilter();
filter.addAction(CALL_FROM_PLUGIN);
registerReceiver(mBroadcastReceiver, filter);
and then implemented a BroadcastReceiver:
BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG, "BroadcastReceiver.onReceive()");
if (CALL_FROM_PLUGIN.equals(action)) {
Log.d(TAG, "Received call from Plugin");
// DO YOUR STUFF HERE
}
}
};
Someone else might be able to point out why this is necessary from a framework point of view, but this is the way I understand that Activities should be called. I hope this applies to your plugin class the way it does with my service class!
Im am trying to pass an instance of my activity to an intent service. The reason for this is the intent service does a lot of background server communication and if there is an network error or the server returns an error I want to display a pop up message.
When i create the service i use this
Intent service = new Intent(this, SyncService.class);
Bundle b2 = new Bundle();
b2.putParcelable(StringsConfig.OBJECT_DELIVERABLES, objects);
service.putExtras(b2);
startService(service);
Is there a way to pass an instance of an Activity over to it. I also have a method inside the SyncService class that accept an Activity but i dont know how to create an instance of the sync service class, pass the activity over via the method, and then start the sync service.
Any help is appreciated.
Its not a great idea to pass an Activity instance to an Intent Service. If your long running Background Service needs to show a dialog message, you are much better off modelling it as an Intent.
Just do:
Intent dialogIntent = new Intent(getApplicationContext(), YourDialogActivity.class);
dialogIntent.putStringExtra(Constants.TITLE, "Your Dialog Title");
dialogIntent.putIntExtra(Constants.MESSAGE, R.string.yourErrorMessageId);
startActivity(dialogIntent);
That way, the service contract is a lot cleaner.
The recommended way for an IntentService to communicate to an activity is via BroadcastReceiver. Take a look at this example:
In the activity that you want your IntentService to communicate with, create a BroadcastReceiver that listens for a specific intent action (a String). Here my example is called batchProcessReceiver, and listens for the BATCH_PROCESS_RECEIVER action. BATCH_PROCESS_RECEIVER can be a public static constant in your Activity.
private BroadcastReceiver batchProcessReceiver = new BroadcastReceiver() {
#Override public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(BATCH_PROCESS_RECEIVER)) {
// do what you need to do here
}
}
};
In your activity's onResume:
registerReceiver(batchProcessReceiver, new IntentFilter(BATCH_PROCESS_RECEIVER));
onPause:
unregisterReceiver(batchProcessReceiver);
Then at a point in your IntentService, you can do
sendBroadcast(new Intent(MyActivity.BATCH_PROCESS_RECEIVER));
to trigger the action you want to do in your activity.
I'm trying to simply set a proximity later for an area an for testing, I simply added this to the onCreate method of my main activity.
public void onCreate(Bundle bndBundle) {
IntentFilter filter = new IntentFilter(WidgetService.ACTION_STOP_PROXIMITY);
registerReceiver(new ProximityIntentReceiver(), filter);
LocationManager locManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
Intent ittIntent = new Intent(this, ProximityIntentReceiver.class);
ittIntent.putExtra(WidgetService.KEY_STOP_IDENTIFIER, 1000);
PendingIntent pitIntent = PendingIntent.getBroadcast(this, 0, ittIntent, 0);
locManager.addProximityAlert(60.15769, 24.94150, 150, -1, pitIntent);
super.onCreate(bndBundle);
getActionBar().setDisplayHomeAsUpEnabled(false);
}
..and here's the simple receiver class that I'm using
public class ProximityIntentReceiver extends BroadcastReceiver {
private static final int NOTIFICATION_ID = 1000;
#Override
public void onReceive(Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
Boolean entering = intent.getBooleanExtra(key, false);
if (entering) {
Log.d(getClass().getSimpleName(), "entering");
}
else {
Log.d(getClass().getSimpleName(), "exiting");
}
}
}
I'm testing this on my emulator and when I use the DDMS console to set the co-ordinates of the phone manually, I still don't see the log message.
My manifest file doesn't have any special code. I've added the correct permissions and have the code for a simple activity- no services or anything.
I read through a whole bunch of posts on StacKOverflow but I haven't been able to resolve the issue. Am I missing something in my snippet?
You are registering this receiver dynamically, through registerReceiver(), to have it respond to broadcasts whose action string is WidgetService.ACTION_STOP_PROXIMITY.
However, the actual broadcast you are sending is trying to use an explicit Intent, identifying your receiver class. This does not line up with the IntentFilter that you are using with registerReceiver().
Either:
Register your receiver in the manifest and get rid of registerReceiver(), in which case your explicit Intent will work, or
Use new Intent(WidgetService.ACTION_STOP_PROXIMITY) instead of new Intent(this, ProximityIntentReceiver.class), so your Intent lines up with your IntentFilter
You cannot use explicit Intent objects to send broadcasts to receivers registered via registerReceiver(). An explicit Intent will only work with a manifest-registered receiver.
make sure you type in the right coordinates. in DDMS they're reversed, longitude first, then latitude