How to know a call is reaching voice mail in android - android

I would like to launch some other apps if my call is reaching others voicemail. How to detect it in android?
Thanks

You could listen to the broadcast of LISTEN_MESSAGE_WAITING_INDICATOR and then act upon it
http://developer.android.com/reference/android/telephony/PhoneStateListener.html#LISTEN_MESSAGE_WAITING_INDICATOR
You would extend PhoneStateListener and then initialize the class
public class VoicemailListener extends PhoneStateListener {
private final Context context;
public VoicemailListener(Context context) {
this.context = context;
}
#Override
public void onMessageWaitingIndicatorChanged(boolean mwi) {
//Message Recieved, do your work here
}
}

Related

LifeCycleOwner in Broadcast reciever

I am using Android Room to store my application data. After device reboot I need to retrieve them and do some functions. I am using LiveData for getting data from database. But I cannot bind Broadcast Reciever as owner. How should I resolve this problem?
public class BootReciever extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
if (context != null) {
ProductUIRepository mRepository = new ProductUIRepository(context.getApplicationContext());
mRepository.findAllProducts().observe(this, new android.arch.lifecycle.Observer<List<ProductUI>>() {
#Override
public void onChanged(#Nullable List<ProductUI> productUIS) {
NotificationMan.setAlarmForProducts(context, productUIS);
}
});
}
}
}
using "this" makes that applications isn't compiling.

Calling a activity method from a BroadCast Receiver

I am looking around on how to call a method which is there in an AbstractActivity from a BroadCast Receiver. I tried making the method static and calling it straight away from the broadcast receiver just by using the classname. But the app then crashes with a nullpointer exception in the switch statement which is there in the static method.
Example:
public class MainActivity extends AbstractActivity{
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public static void additionDone(Context context,int number){
switch(number)
case 1:
Toast.makeText(context, "First case" , Toast.LENGTH_SHORT).show();
break;
}
}
BroadCast Receiver example:
public class receiver extends BroadcastReciever{
#Override
public void onReceive(Context context, Intent intent) {
//logic
}
public class anotherMethod(){
MainActivity.additionDone(1,context);
}
}
Is something wrong with the code above? Or is there a different way of calling a static method which belongs to a abstract activity?
The order of the parameters/arguments don't match.
additionDone(Context context,int number)
MainActivity.additionDone(1,context)
Your Context context; of the MainActivity is never assigned,
change your code to this
public static void additionDone(int number, Context context){
...//work with that context
}
and in your reciever
MainActivity.additionDone(1, context); //call with the context you got in your receiver constructor

Android. How to Get context from different activity

I'm playing with the GCM.
Everything is perfect using the example on https://code.google.com/p/gcm/source/checkout
Im getting notifications on my app with the gcm messages,
Now I want to add the message in a listView located on my MainActivity.
Im receiving my messages on a different class (GcmIntentService.java). How can I get MainActivity context to sendBroadcast.
Already tried with
private static Context mContext;
public static Context getContext() {
return mContext;
}
public static void setContext(Context context) {
mContext = context;
}
But is not working.
Any Ideas.
Thanks
I am not sure what you are doing. But keeping the below in mind
Do not keep long-lived references to a context-activity (a reference to an activity should have the same life cycle as the activity itself).
http://www.curious-creature.org/2008/12/18/avoid-memory-leaks-on-android/
You can do as below
Example:
new MyClass(ActivityName.this);
class MyClass
{
Context mContext;
public MyClass(Context context)
{
mContext=context;
}
}
pass the context variable through constructor .
create new activity like below
public class GetContext extends AppCompatActivity {
Context mainActivity;
public GetContext(Context mainActivity){
this.mainActivity = mainActivity;
}
and in your previous mainActivity send this context as below
GetContext sendContext = new GetContext(mainActivityContext);
where mainActivityContext is Context mainActivityContext = this;
or simply pass this instead of mainActivityContext
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_attendance_from_database);
}

How to Interface onReceive Method

I've set up a class called NetworkStatus that monitors the network status of the device. Within NetworkStatus I've defined a BroadcastReceiver that monitors if there has been a connectivity change, i.e., if the internet has been switched off or on. I want to 'expose' this method (i.e. onReceive) to the activity that instantiated the instance of NetworkStatus.
I'm trying to achieve this by setting up an interface in the NetworkStatus class, but I'm not sure how to call the interface from the onReceive method of the BroadcastReceiver i.e.,
public class NetworkStatus {
public static interface NetworkStatusCallbacks {
void onReceive();
}
public static class ConnectivityChange extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
// need to call NetworkStatusCallbacks onReceive here
}
}
}
Then in the main activity I would do something like,
public class MyActivity extends Activity implements NetworkStatus.NetworkStatusCallbacks {
#Override
public void onReceive() {
// Do stuff here
}
}
Would really appreciate some pointers. Thanks.
Possible Solution
Think I have found a solution. As Selvin pointed out, to do this requires the Activity. I therefore obtained the Activity by casting it from the activity context that is passed to a constructor for the NetworkStatus class, i.e., setting a constructor for the class as
private static nsc NetworkStatusCallbacks;
public NetworkStatus (Context context) {
Activity activity = (Activity) context;
nsc = (NetworkStatusCallbacks) activity;
}
I can then call the interface from the onReceive method of the BroadcastReceiver as follows:
public void onReceive(Context context, Intent intent) {
nsc.onReceive();
}
Try something like this:
public class NetworkStatus {
public static interface NetworkStatusCallbacks {
void onReceive();
}
private final NetworkStatusCallbacks m_cb;
NetworkStatus(NetworkStatusCallbacks cb)
{
m_cb=cb;
}
public static class ConnectivityChange extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
// need to call NetworkStatusCallbacks onReceive here
m_cb.onReceive()
}
}
}

The right way to dynamically control location tracking (registering / unregistering location broadcast receiver)

I want to dynamically control location tracking (registering / unregistering location broadcast receiver). This is how I am planning to do it. I have two questions :
What are the mistakes in the implementation below because all this concept is still very theoretical to me as I am very new to android/java dev. Still building concepts!
How do I pass some EXTRA_INFO from my location library class to the location receiver.
IMPLEMENTATION:
I have a library class LocationLibrary.java which consists of two methods. They do as the name suggest. The location tracking should start when I call startTracking(). Plz note the extraInfo that needs to be passed to myLocationReceiver. The tracking should stop when stopTracking() is called.
Code snippet:
public class LocationLibraray
{
private static BroadcastReceiver myLocationReceiver;
public LocationLibraray(Context context)
{
this.ctx = context;
myLocationReceiver = new MyLocationReceiver();
}
public void startTracking(Context context, String extraInfo)
{
IntentFilter filter = new IntentFilter();
filter.addAction("com.app.android.tracker.LOCATION_READY");
context.registerReceiver(myLocationReceiver, filter);
// NEED TO PASS extraInfo to myLocationReceiver for some processing, but HOW?
}
public void stopTracking(Context context)
{
context.unregisterReceiver(locationReceiver);
}
}
MyLocationReceiver.java
public class MyLocationReceiver extends BroadcastReceiver {
public void onReceive(final Context context, Intent intent) {
if ((intent.getAction() != null) &&
(intent.getAction().equals("com.app.android.tracker.LOCATION_READY")))
{
//GET THAT EXTRA INFO FROM LocationLibrary class and process it here
}
}
}
Please help me out. Thnx!
Why not add a constructor to MyLocationReceiver?
public class MyLocationReceiver extends BroadcastReceiver {
String info = "";
public MyLocationReceiver(String extraInfo)
{
this.info = extraInfo;
}
........
public void onReceive(final Context context, Intent intent) {
if ((intent.getAction() != null) &&
(intent.getAction().equals("com.app.android.tracker.LOCATION_READY")))
{
if (info.contains("Hi"))
//do some stuff
}
}
}
And you would instantiate it like this:
myLocationReceiver = new MyLocationReceiver(new String("Hello!"));

Categories

Resources