I am running my activity and this has a Broadcast receiver which listens to ble updates from a Ble service class. When the device is disconnected when in not in proximity I am using AltBeacon to start my application when the device is in region and it connects to the device. But the Broadcast receiver is not started.
My code is something as follows:
class MyActivity{
onCreate(){
}
onResume(){
registerReciever(....);
}
private final BroadcastReceiver gattReceiver = new BroadcastReceiver(){
onReceive(Context context, Intent intent){
}
}
}
Related
My Android application is continuously using the microphone and wants to release the microphone to other applications such as VOIP Calls.
I have registered broadcast receiver in the Manifest. But the microphone is not getting released.No sim card in the phones, I am only interested in detecting SIP sessions.
Here is my code in the Manifest
<receiver
android:name=".SipCallReceiver"
android:enabled="true"
android:exported="true">
</receiver>
Broadcastreceiver class
public class SipCallReceiver extends BroadcastReceiver {
ListeningActivity LA = new ListeningActivity();
#Override
public void onReceive(Context context, Intent intent) {
SipAudioCall call = null;
final SipAudioCall.Listener listener = new SipAudioCall.Listener() {
#Override
public void onCallEnded(SipAudioCall call) {
LA.setflgphone(false); LA.onPstop();
}
#Override
public void onCallEstablished (SipAudioCall call) {
LA.setflgphone(true); LA.Lstop();
}
};
}
}
Can anyone please let me know the mistake I am making here.
Thank you
I was able to solve it.
I created two services.
The first service monitors the Audio Manager at schedule interval with ScheduledExecutorService. This will get the mode of the Audio Manager.
When there is no communication, the mode will be 0.
This status broadcast to a locally registered broadcast receiver. The broadcast receiver will stop and start the second service which using the microphone depending on the audio mode.
I have a Service that scans for BLE devices. The Activity should show some data gathered by the Service.
A Receiver has been implemented, to be notified when the Bluetooth is enabled, so that we know when to start the Service.
If the Service is running, and the Activity is opened, it just executes bindService(). However, if the Service isn't running (because the Bluetooth is disabled), the App is opened and the Bluetooth is enabled, it won't bind because the binding process has already been skipped.
How can I be notified about the Service starting or automatically binding when started?
Thank you.
You can use the LocalBroadCastManager to send a broadCast from your service to your activity.
Helper to register for and send broadcasts of Intents to local objects within your process. This has a number of advantages over sending global broadcasts with sendBroadcast(Intent):
You can use localbroadcast reciever from your service.
In your service use these code
intent = new Intent("my-integer");
intent.putExtra("message",""+a);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
In your activity use this code
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Extract data included in the Intent
String data = intent.getStringExtra("message");
if (!data.equals("0")) {
//Do something
} else {
//Do something else
}
}
}
};
i am new to Android and trying to understand the communication between apps.
I am trying to write 3 little apps which can communicate with each other. If you want to sent a message to everybody you just use an implicit broadcast.
implicit Intent intent.setAction("com.example.myChatMessage")
if you want to adress only 1 specifc receiver i did it with
explicite Intentintent.setComponent("com.example.test.android.broadcastreceiver.b",
"com.example.test.android.broadcastreceiver.b.myBroadcastReceiver")
this works, when the broadcast receiver is a seperate class and defined in the AndroidManifest.xml.
My Question: Is it possible to explicit adress a dynamicall registered broadcast receiver?
package com.example.test.android.broadcastreceiver.b;
public class MainActivity extends Activity {
private final IntentFilter intentfilter = new IntentFilter("com.example.myChatMessage");
private myBroadcastReceiver broadcastreceiver;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
broadcastreceiver = new myBroadcastReceiver();
registerReceiver(broadcastreceiver, intentfilter);
}
public static class myBroadcastReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra("message");
Log.d("message", "B received: "+message);
}
}
}
It receives all implicit broadcasts but no explicit one - i don't know hot to adress it. Can you help me?
It does not appear possible to send an explicit intent to a dynamically registered broadcast receiver. Registering the receiver in AndroidManifest.xml is the only way.
If you dynamically register a BroadcastReceiver – by calling Context.registerReceiver() – you supply a BroadcastReceiver instance ... If you try to send an Intent to the receiver by naming the class of the BroadcastReceiver, it will never get delivered .. The Android system will not match the Intent you declared to the class of the BroadcastReceiver instance you registered.
Source: http://onemikro2nd.blogspot.com/2013/09/darker-corners-of-android.html
i develop an Android-Bluetooth App with 3-4 Activitys. Now i have to receive bluetooth data in any of these activitys.
I think i have to implement a Service which contains a BroadcastReceiver which listens to incoming BlueTooth Data and send a Broadcast, but i don't know how to do that.
Thanks in advance.
You can implement your own BroadcastReceiver. So, when your LocalService receive a data, it will notify using sendBroadcast method. Your activities should register the specific BrodcastReceiver.
In your Service
Notify about received messages:
public void onMessageReceived(String message) {
Intent intent = new Intent(ACTION_BLUETOOTH_MESSAGE);
intent.putExtra(BLUETOOTH_MESSAGE_CONTENT, message);
sendBroadcast(intent);
}
On each activity
Registering the broadcast receiver:
registerReceiver(messageReceiver,
new IntentFilter(ACTION_BLUETOOTH_MESSAGE));
Implementation of the broadcast receiver:
private BroadcastReceiver messageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra(BLUETOOTH_MESSAGE_CONTENT);
//Do something you want
}
};
I've a local service that is started in my main activity. The service is responsible for network I/O. It also interacts with other activities in my application. Till, now the service was only "providing" data to activity (using callbacks and not Intents.)
Now I need to send data (custom objects) from my activities to the service. I understand one way to do it using Intents. I know it is possible to receive an Intent in the service by overriding the onStartCommand() in my service. But it's not clear to me if the onStartCommand will be invoked every time I broadcast an Intent from my sending activity. Can a Service also be BroadcastReceiver ? If yes - how ?
Thanks.
You can create a BroadcastReceiver object in the service and register it to listen to any broadcast event you want. It's something like this:
BroadcastReceiver mScreenStateReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//handle the broadcast event here
}
};
#Override
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
registerReceiver(mScreenStateReceiver, filter);
}
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(mScreenStateReceiver);
}
Regards,