Send string from service to activity

夙愿已清 提交于 2019-11-28 09:14:31

LocalBroadcastManager is the best solution for solving this type of problem. I have implemented LocalBroadcastManager to your existing. Its very easy. Hope it will work. You code will look something like this

private BroadcastReceiver statusReceiver;
private IntentFilter mIntent;
Sensor accelerometer;
SensorManager sm;
TextView acceleration;
SendValues sv;
int counter3 = 0;
int counter5 = 0;

private static final String TAG = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     //mIntent = new IntentFilter("NOW");

  // this.registerReceiver(,new IntentFilter("status"));

    sm = (SensorManager) getSystemService(SENSOR_SERVICE);
    sm.registerListener(this,accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    acceleration = (TextView) findViewById(R.id.sensorTxt);

}

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String type = intent.getStringExtra("message");  //get the type of message from MyGcmListenerService 1 - lock or 0 -Unlock
        sm = (SensorManager) getSystemService(SENSOR_SERVICE);
        accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        Log.d(TAG, "Status: " + type);
        if (type == "1") // 1 == lock
        {
            Toast.makeText(getApplication(), "Lock", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplication(), "UNLOCK", Toast.LENGTH_LONG).show();
        }
    }
};

@Override
 protected void onResume()
 {
    super.onResume();
    //registerReceiver(statusReceiver,mIntent);
    LocalBroadcastManager.getInstance(MainActivity.this).registerReceiver(broadcastReceiver, new IntentFilter("NOW"));
  }

@Override
protected void onPause() {
    if(mIntent != null) {
        unregisterReceiver(statusReceiver);
        mIntent = null;
    }
    super.onPause();
}

And pass your value from push notification something like this

Intent in = new Intent();
in.putExtra("TYPE",typemessage);
in.setAction("NOW");
//sendBroadcast(in);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(in);

you can user LocalBroadcastManager to achieve what you want.like in your service call this method when you want to send the data.

private static void sendMessageToActivity(String msg) {
Intent intent = new Intent("intentKey");
// You can also include some extra data.
intent.putExtra("key", msg);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}

In your Activity register a Receiver in onCreate()

LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
        mMessageReceiver, new IntentFilter("intentKey"));

and out of onCreate() this code.

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
    // Get extra data included in the Intent
    String message = intent.getStringExtra("key");
    tvStatus.setText(message);
    // Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
};

hope you got an idea.

you can do 2 things :

1. you can make a class and implement Parcelable .
2. use an Intent service instead of Service .

Refer below link : http://android-er.blogspot.in/2013/03/send-data-from-intentservice-to.html http://www.sanfoundry.com/java-android-program-send-data-service-activity/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!