In my android project there is one activity and on service. in the main layout i have three control : 2 buttons (start and stop button) and a TextView (for the result).when i click start button the service will start and sends a text message via localbroadcastmanager to the main activity and activity must show the message in the TextView but this is not happening. i also should add that i have added Android Support Library v4 to my project and i receive no errors and crashes when i run the application. i have tested these code on a real device and genymotion but i didn't get the desired result. i would if you tell what is wrong with my codes: Here is my Activity code :
package ir.sanatnegar.lbcm;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class LBCMActivity extends Activity {
Button btnStart;
Button btnStop;
TextView tvMessage;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("SAED"));
btnStart = (Button) findViewById(R.id.btnStart);
btnStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
startService(new Intent(LBCMActivity.this, MyService.class));
}
});
btnStop = (Button) findViewById(R.id.btnStop);
btnStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stopService(new Intent(LBCMActivity.this, MyService.class));
}
});
}
@Override
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onPause();
}
@Override
protected void onResume() {
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("SAED"));
super.onResume();
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("LOG", "Message Received ... ");
String message = intent.getStringExtra("message");
tvMessage.setText(message);
}
};
}
// The Service Code :
package ir.sanatnegar.lbcm;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
public class MyService extends Service {
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LOG", "onStartCommand occured");
sendMessage();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.i("LOG", "onDestroty occured");
super.onDestroy();
}
private void sendMessage()
{
Log.i("LOG", "Broadcasting Message ...");
Intent intent = new Intent("SAED");
intent.putExtra("message", "This is a message from MyService!!!");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}