In my application I use a service that connects to specified device and receives some data in the form of strings.
When it gets some special strings it throws a notification to the user.
When the user clicks on the notification it takes him to a new activity, where the values received from the bluetooth are displayed. What I want to do is to update the textView with the values received from the bluetooth.
I have looked up the net and I found out that my best shot is BroadcastReceiver. I have this handler in onCreate which handles the messages received from the Bluetooth. What i want to do is when I receive a new string to pass it to my Activity:
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 1) {
connectStat = true;
}else if(msg.what == MESSAGE_RECEIVE){
byte[] readBuf = (byte[]) msg.obj;
String strIncom = new String(readBuf, 0, msg.arg1);
sb.append(strIncom);
int endOfLineIndex = sb.indexOf("/");
if (endOfLineIndex > 0) {
String sbprint = sb.substring(0, endOfLineIndex);
Intent in = new Intent();
in.setAction(BROADCAST_ACTION);
in.putExtra(AlarmService.CO_CONSENTRATION_NUMBER, sbprint);
sendBroadcast(in);
if(sbprint.equals("4")){
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(9999);
Notification notification = new Notification(R.drawable.gasmask, "New Message", System.currentTimeMillis());
notification.when = System.currentTimeMillis();
Intent notificationIntent = new Intent(AlarmService.this, ActivityCOConsentration.class);
String value = sbprint;
notificationIntent.putExtra(AlarmService.CO_CONSENTRATION_NUMBER, value);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(AlarmService.this, 0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(AlarmService.this, "Alarming CO level", "Level of CO is alarming. Take action.", pendingIntent);
notificationManager.notify(9999, notification);
myclip.start();
sb.delete(0, sb.length());
}else if(sbprint.equals("8")){
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(9999);
Notification notification = new Notification(R.drawable.gasmask, "New Message", System.currentTimeMillis());
notification.when = System.currentTimeMillis();
Intent notificationIntent = new Intent(AlarmService.this, ActivityCOConsentration.class);
String value = "8";
notificationIntent.putExtra(AlarmService.CO_CONSENTRATION_NUMBER, value);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(AlarmService.this, 0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(AlarmService.this, "Alarming CO level", "Level of CO is life endagering. Sending out SOS sms.", pendingIntent);
notificationManager.notify(9999, notification);
myclip.start();
disconnect();
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sb.delete(0, sb.length());
}
}
}
}
};
Here is my activity that is supposed to receive the data:
public class ActivityCOConsentration extends Activity{
private TextView co_concentration;
private MyReceiver mMyReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_coconsentration);
co_concentration = (TextView) findViewById(R.id.co_number);
onNewIntent(getIntent());
mMyReceiver = new MyReceiver();
registerIntents();
}
public void onNewIntent(Intent intent){
Bundle extras = intent.getExtras();
String covalue = extras.getString(AlarmService.CO_CONSENTRATION_NUMBER);
co_concentration.setText(covalue);
if(covalue.equals("8")){
Intent i = new Intent(this, AlarmService.class);
stopService(i);
}
}
private void registerIntents(){
IntentFilter filter = new IntentFilter();
filter.addAction(AlarmService.BROADCAST_ACTION);
getApplicationContext().registerReceiver(mMyReceiver, filter);
}
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(AlarmService.BROADCAST_ACTION)) {
String v = intent.getExtras().getString(AlarmService.CO_CONSENTRATION_NUMBER);
co_concentration.setText(v);
}
}
}
public void onDestroy(){
super.onDestroy();
stopService(new Intent(AlarmService.ALARM_SERVICE));
getApplicationContext().unregisterReceiver(mMyReceiver);
}
}
What happens is that I get only one new string and then nothing.
Any help will be very usefull.