1

I have a service that listens for (ON_BATTERY_CHANGE), then onReceive service sends a Broadcast to My MainActivity. The problem is that I somehow can't get them from service to my main activity. Code: Main Activity:

public class MainActivity extends Activity
  private BroadcastReceiver batteryReceiverService;
  private TextView text2;
....
protected void onCreate(Bundle savedInstanceState) {
text2=(TextView)findViewById(R.id.TV_text2);
batteryReceiverService = new BroadcastReceiver(){
            @Override
            public void onReceive(Context context, Intent intent) {

                text2.setText("left: "+intent.getStringExtra("H")+" hours "+intent.getStringExtra("M")+" minute(s)");
                    Log.e("text2","text2 HHH " +intent.getStringExtra("H")); //log shows 0
                    Log.e("text2","text2 MMM " +intent.getStringExtra("H")); // log shows 0
            }
        };
        registerReceiver(batteryReceiverService, new IntentFilter(UltimateBatterySaverService.BROADCAST_ACTION));
....
@Override
    protected void onDestroy() {

        unregisterReceiver(batteryReceiverService);
        super.onDestroy();
    }

Service:

public class UltimateBatterySaverService extends Service {
private Intent intent;
    static final String BROADCAST_ACTION = "lt.whitegroup.ultimatebatterysaver";
    private BroadcastReceiver batteryLevelReceiver;
....
public void onCreate() {
        super.onCreate();
        intent = new Intent(BROADCAST_ACTION); 

    }
@Override
    public void onDestroy() {
        unregisterReceiver(batteryLevelReceiver);
        super.onDestroy();

    }
IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);


        batteryLevelReceiver = new BroadcastReceiver(){
            @Override
            public void onReceive(Context context, Intent intent){

// Receiving data, calculating and etc
averageChargingH=timeAllInHours;
averageChargingM=timeAllInMinutes;
// to put extras and send broadcast
does();
......

public void does(){
        String strLong = Long.toString(averageChargingH);
        String strLong2 = Long.toString(averageChargingM);
        Log.e("cccccc","strLong h "+strLong); // getting good value not 0(everything ok)
        Log.e("cccccc","strLong2 m"+strLong2); // getting good value not 0(everything ok)
        intent.putExtra("H", strLong);
        intent.putExtra("M", strLong2);
        sendBroadcast(intent);
    }

Any ideas why my information is not transfered correctly?

whiteLT
  • 338
  • 7
  • 21
  • Did you add this service to AndroidManifest.xml ? – hakki Mar 16 '13 at 14:10
  • As far as I know if we are registering it in Activity, we do not need to do that in XML or am I wrong? – whiteLT Mar 16 '13 at 14:13
  • See: http://stackoverflow.com/questions/7144908/how-is-an-intent-service-declared-in-the-android-manifest – hakki Mar 16 '13 at 14:15
  • Ohh... sorry I mislead you, yes service is declared in manifest. I thought you were talking about Broadcast receiver. – whiteLT Mar 16 '13 at 14:20

1 Answers1

1

The does() method seems to be using variables in the same scope as onReceive so I'm guessing that the intent variable in does() is actually the Intent passed in from onReceive.

Try adding some logging before sending the broadcast to check if the action of the intent is correct, or simply create the broadcast intent in the onReceive method and name it intent2.

Carlo B.
  • 1,183
  • 1
  • 11
  • 23
  • Are you talking about that logging that I am doing in does method? `Log.e("cccccc","strLong h "+strLong); // getting good value not 0(everything ok)// getting good value not 0(everything ok)` `Log.e("cccccc","strLong2 m"+strLong2);` – whiteLT Mar 16 '13 at 15:30
  • add Log.d("cccccc", intent.getAction()); and if it logs as android.intent.action.BATTERY_CHANGED then you are sending the wrong intent – Carlo B. Mar 16 '13 at 15:34
  • it only logged as my package name – whiteLT Mar 16 '13 at 15:44
  • 1
    A couple more ideas: change intent.getStringExtra("H") to intent.getExtras().get("H"). Also try changing BROADCAST_ACTION from "lt.whitegroup.ultimatebatterysaver" to "lt.whitegroup.ultimatebatterysaver.BATTERY_CHANGED" – Carlo B. Mar 16 '13 at 16:25
  • 1.change intent.getStringExtra("H") to intent.getExtras().get("H") Log now shows correct data, but don't know why my text view only shows up for 1 sec and then disappears. 2.Also try changing BROADCAST_ACTION from "lt.whitegroup.ultimatebatterysaver" to "lt.whitegroup.ultimatebatterysaver.BATTERY_CHANGED" AWNSER: logged as lt.whitegroup.ultimatebatterysaver.BATTERY_CHANGED – whiteLT Mar 16 '13 at 16:43
  • Ohh... I know why appearing only 1sec, yes it is the other code part where I recreate activity, but now I don't need it, so everything working fine!!! Thank you. By the way I have another question about shared prefs. Before intent I was using shared prefs to save and display info, but that data somehow were incorrect at start and then after 2-3 receives it was showing correct one :). Do you know why that could happen with shaded prefs? – whiteLT Mar 16 '13 at 16:44
  • I'm not sure, but if I had to guess it would be that the editor was not committing the data properly after putting new values in. – Carlo B. Mar 16 '13 at 16:58
  • Ok, nevermind that :), thank you for help, so my mistake was in getting extras content? Bad syntax? – whiteLT Mar 16 '13 at 17:11