1

I'm on a activity with a textview and in the background is AlarmReceiver running with a 10min time trigger. The activity has a method which I want to call by the trigger. The method sets a new value to the textview. But by calling it from the trigger I can not use "findViewById". I get a NullPointerException at this point. I also tried to setContentView when its called by the trigger but also here I get an NullPointerException

This it the Code:

ValuesActivity:

public void setSyncValue(Context context, boolean fromSyncService, String value){
    try {
        if(fromSyncService){
            setContentView(R.layout.activity_values);
        }
        ...
        try{
            ...
            TextView lastSyncTV = (TextView) findViewById(R.id.last_sync_label);
            lastSyncTV.setText(value);
            ...
        } catch (Exception ex) {
            ....
        }
    } catch (Exception ex) {
        ....
    }
}

AlarmReceiver

@Override
public void onReceive(Context context, Intent intent) {
    try {

        Intent i = new Intent(context, SyncService.class);
        context.startService(i);
    }catch (Exception ex){
        .....
    }
}

SyncService

  //runns every 10min
  @Override
  protected void onHandleIntent(Intent intent) {

    try {
        ...
        ValuesActivity valuesActivity = new ValuesActivity();
        valuesActivity.setSyncValue(....)
        ....
    }
    .....
  }
WeSt
  • 889
  • 5
  • 14
  • 32
  • See here https://stackoverflow.com/questions/16934425/call-an-activity-method-from-a-broadcastreceiver-class – Rishikesh pathak Oct 18 '18 at 08:29
  • Possible duplicate of [Call an activity method from a BroadcastReceiver class](https://stackoverflow.com/questions/16934425/call-an-activity-method-from-a-broadcastreceiver-class) – Rishikesh pathak Oct 18 '18 at 08:30
  • Sorry, it's not directly called in the alarmReceiver. Actually it's called in a method which is called by the alarmReceiver. I updated my Quest, please see. thanks – WeSt Oct 18 '18 at 09:37

2 Answers2

1

actually you can't create an activity by creating an instance of that, you must run an activity by Intents. in your case if an activity is already in foreground and visible to user you must start activity with a FLAG_ACTIVITY_CLEAR_TOP flag. that send new intent to your current visible activity. to do this you can use this code:

context.startActivity(new Intent(this,ValuesActivity.class).putExtras(bundle).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));

in this way if activity already visible to user then android instead of recreating activity just send a new intent to method onNewIntent and you must override this method in your ValuesActivity to receive new intent sent by your service. something like this:

public void onNewIntent(Intent intent){
    String value = intent.getStringExtra(/*your key*/);
    lastSyncTV.setText(value);

} 

if you want to check activity visibility you can add static property in your activity public static boolean isVisible = false; and toggle it true in onResume of your activity and toggle to false in onPause method. then before startActivity just check if(ValuesActivity.isVisible).

nariman amani
  • 263
  • 6
  • 21
  • This could be a possible solution. Is there a option to hide the animation when the new intent is loaded? – WeSt Oct 18 '18 at 10:30
  • EDIT: This will not work. If I'm on a different activity the ValuesActivity will be startet every 10min. – WeSt Oct 18 '18 at 10:47
  • @WeSt if your ValuesActivity is not visible why you want to change it? – nariman amani Oct 18 '18 at 13:47
  • I just want to change it if the ValueActivity is visible. But with you solution the ValuesActivity will be started no matter what activity I am on – WeSt Oct 18 '18 at 14:21
0

Inside your

@Override
public void onReceive(Context context, Intent intent)

use

Intent intent  = new Intent("alaram_received");
context.sendBroadcast(intent);

Inside your mainactivity first register the broadcast

IntentFilter intentFilter = new IntentFilter("alaram_received");
registerReceiver(alarm_receiver,intentFilter);

and then call your method in here:

BroadcastReceiver alarm_receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // your logic here
        Log.i("alarm_received","success");

    }
};
hossam scott
  • 466
  • 5
  • 28
  • Sorry, it's not directly called in the alarmReceiver. Actually it's called in a method which is called by the alarmReceiver. I updated my Quest, please see. thanks – WeSt Oct 18 '18 at 09:37
  • 1
    can i know why do u need to run in in SyncService? need to get better view of what u are trying to do to be able to help you. – hossam scott Oct 18 '18 at 09:43
  • there are also some other async executions and network/wifi handler in syncservice – WeSt Oct 18 '18 at 10:07