Android wearableListenerService invoke method from activity - android

Is there a way to call a non-static method of an activity from a wearableListenerService?
(Binding the service is not possible for wearableListenerService. Because the method onBind() is final.)
Simplified example:
Service.class:
public class MyService extends WearableListenerService {
#Override
public void onMessageReceived(MessageEvent messageEvent){
myActivityMethod();
}
...
}
Activity.class:
public class MyActivity extends Activity {
public void myActivityMethod(){
//do sth.
}
...
}

you could Broadcast an event and register a BroadcastReceiver in your Activity. You could also connect the GooglePlayClient in your Activity and let it handle the onMessageReceived event, if the current activity is the current at screen

Related

Notify when onPause() called in any activity of an application

I think this question may simple but I didn't find any solution for this,
I there any way in Android that if any one of an activity calls onPause() I need to show Toast message or any notification kind of thing need to show. Generally I want to get notified when activity calls onPause() but I need it in one place since I may have some 15 activity I don't want to add it in all the activity.
ex:If I have activity when any one of the activity calls onPause I need to get notified but that notification code should be in one place and we should not add any line of code onPause() Is it possible to do this.
Thanks.
Create a baseActivity, which has for example :
open class BaseActivity : AppCompatActivity() {
override fun onPause() {
super.onPause()
Toast.makeText(this, "notified", Toast.LENGTH_SHORT).show()
}
}
Then you can extends this in your activities and handle the on pause call in BaseActivity
If your minSdkVersion >= 14, you can use Application.ActivityLifecycleCallbacks: ActivityLifecycleCallbacks
You have to define a custom Application class and you can register for this callbacks afterwards:
public class MyApplication extends Application {
private class LifecycleCallbacks implements Application.ActivityLifecycleCallbacks {
#Override
public void onActivityCreated(final Activity activity, final Bundle savedInstanceState) {
//nothing to do
}
#Override
public void onActivityDestroyed(final Activity activity) {
//nothing to do
}
#Override
public void onActivityPaused(final Activity activity) {
// TODO Do your stuff, e.g. show toast.
}
#Override
public void onActivityResumed(final Activity activity) {
//nothing to do
}
#Override
public void onActivitySaveInstanceState(final Activity activity, final Bundle outState) {
//nothing to do
}
#Override
public void onActivityStarted(final Activity activity) {
}
#Override
public void onActivityStopped(final Activity activity) {
}
}
private final LifecycleCallbacks callbacks;
public void onCreate() {
super.onCreate();
callbacks = new LifecycleCallbacks();
application.registerActivityLifecycleCallbacks(callbacks);
}
}
Create a BaseActivity which contain all the methods you want to use in all other activities.
Then extend every activity with BaseActivity to call onPause() method.

Overriding method in secondary class

I have a super-class called MainActivity and a secondary class called Insert.
The two class are defined as the code below.The Insert class extends the MainActivity class.
In the two class I have the onStop() method, and in the Insert class I have #Override. The problem is that when onStop is executed in the Insert class, also the onStop method of MainActivity is called. Why? How can I do?
Thank you!
public class MainActivity extends AppCompatActivity {
public void onStop() {
//some code
super.onStop();
}
}
public class Insert extends MainActivity {
#Override
public void onStop() {
//some code
super.onStop();
}
}
onStop is the default method provided by activity lifecycle. You can have your custom method name say onMyStop() and call that method from Insert class's onStop using super.onMyStop()
The reason is calling super.onStop() in Insert overriden method. But calling super.onStop is mandatory. To fix it, you can create another method like doOnStop in MainActivity, call it inside onStop of MainActivity and override in Insert without calling super.doOnStop.
Here is code sampe:
public class MainActivity extends AppCompatActivity {
public void onStop() {
super.onStop();
doOnStop();
//some code
}
public void doOnStop() {
}
}
public class Insert extends MainActivity {
#Override
public void doOnStop() {
//some code
}
}

Access TextViews from broadcast receiver

In my broadcast receiver, I'm accessing a method that calls another method which is in my MainActivity class. The method from my MainActivity class uses variables that are set in the onCreate() method.
The problem I'm having is that when the broadcast receiver tries to access this method when the app has been closed, it finds the variables null because the onCreate method hasn't ran to set the variables.
Should I try to trigger the onCreate method from my broadcast receiver, or do I have this whole setup wrong? I tried to condense the code a bit so it's not too long but below you can see the gist of what I'm trying to do.
The error I'm getting is that "text" and "ringer" are null when trying to setText. This only happens when the broadcast receiver runs while the app is closed and not running.
WifiScanCompleteReceiver code:
public class WifiScanCompleteReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
...
}
public static void activate() {
...
MainActivity.statusText();
}
}
Activity code:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
...
setContentView(R.layout.activity_main);
text = (TextView)findViewById(R.id.state);
ringer = (TextView)findViewById(R.id.mode);
state = mSettings.getString("state", "init");
mode = mSettings.getInt("ringer", 0);
statusText();
}
public static void statusText() {
text.setText(state);
if (mode == 1) {
ringer.setText("Vibrate");
} else if (mode == 2) {
ringer.setText("Normal");
} else {
ringer.setText("Unkown");
}
}
}
Instead of accessing the TextView from the receiver, I just triggered the main activity as a service with context.startService(Intent); and that seemed to get me what I wanted.
I needed to trigger the code in my main activity to run even if the main activity had been stopped. So this solved that for me.
You can solve this using interface:
1) Create an interface
interface MyListener {
public void doSomething();
}
2) Initialize the Listener in BroadcastReceiver
public class WifiScanCompleteReceiver extends BroadcastReceiver {
private MyListener listener;
public void onReceive(Context context, Intent intent) {
listener = (MyListener)context;
listener.doSomething(); // Call interface method
}
}
3) Implement interface in the Activity and override the method
public class MainActivity extends Activity {
// Your Activity code
public static void statusText() {
text.setText(state);
if (mode == 1) {
ringer.setText("Vibrate");
} else if (mode == 2) {
ringer.setText("Normal");
} else {
ringer.setText("Unkown");
}
}
#Override
public void doSomething(){
statusText();
}
}
Relevant Link:
If you want to read the advantage of using interface Read this
Its not a good approach to call activity's method directly from the receiver. App will crash in a case when your activity is not visible, but due to receiver's call it will try to execute activity's code.
You can use local broadcast here. Instead of calling activity's method from receiver send local broadcast, which you need to register in your activity and in receiver of local broadcast call your activity method. This method(Activity's method) call from local broadcast will only execute when your activity will be visible and will not result in app crash.

How to Call an activity method from a BroadcastReceiver?

I am currently developing an app, I need to call an activity method from BroadcastReceiver.
My app is to when the user getting a call Broadcast receiver find that and send a message to that particular number.
I am able to get that no, but I want to send a message to that number, for this sending sms I created an activity in side sending sms method is there..
How do I call that particular method from my BroadcastReceiver.
You need to make that method static and public inside your activity.You can call that method like this:
ActivityName.mehtodName();
It will be much better if you put that method in your util class and call it from there.
You can create a Callback using an interface for calling that the activity method from BroadcastReceiver
interface MyCallback{
public void doStuff();
}
Provide the CallBack in the BroadcastReceiver
public class MyBroadcastReceiver extends BroadcastReceiver{
private MyCallback callback;
public MyBroadcastReceiver(MyCallback callback){
this.callback = callback; //<-- Initialse it
}
#Override
public void onReceive(Context context, Intent intent) {
callback.doStuff(); //<--- Invoke callback method
}
}
Now implement the Callback in your Activity and override the method
MyActvity extends AppcompatActivity implements MyCallback {
// Your Activity related code //
// new MyBroadcastReceiver(this); <-- this is how you create instance
private void sendSMS(){
// your logic to send SMS
}
#Override
public void doStuff(){
sendSMS(); //<--- Call the method to show alert dialog
}
}
You can read the advantage of using this approach here in detail

BroadcastReceiver as inner class

I know the BroadcastReceiver can't be used if defined as Activity's inner class. But I wonder why? Is it because the system would have to instantiate a large Activity object to just have instantiated a receiver instance?
... because the system would have to instantiate a large Activity object to just have instanitated a recevier instance?
Yup, just like any other non-static inner class. It has to get an instance of the outer class from somewhere (e.g. by instantiating or by some other mechanism) before it can create an instances of the (non-static) inner class.
Global broadcast receivers that are invoked from intents in the manifest file that would be be instantiated automatically by the system have no such outer instance to use to create an instance of the broadcast receiver non-static inner class. This is independent of what the outer class is, Activity or not.
However, if you are using a receiver as part of working with an activity, you can manually instantiate a broadcast receiver yourself in the activity (while one of the activity callbacks, you have an instance of the outer class to work with: this) and then register/unregister it as appropriate:
public class MyActivity extends Activity {
private BroadcastReceiver myBroadcastReceiver =
new BroadcastReceiver() {
#Override
public void onReceive(...) {
...
}
});
...
public void onResume() {
super.onResume();
....
registerReceiver(myBroadcastReceiver, intentFilter);
}
public void onPause() {
super.onPause();
...
unregisterReceiver(myBroadcastReceiver);
}
...
}

Categories

Resources