Reputation: 1661
So, I have my application which has BroadcastReceiver and receives notification from other application. The problem is, I am receiving a message, displaying it in the phone via Toast, but I want to set it visible on my TextView. I have only two classes in my whole app.
My receiver is my own class which extends BroadcastReceiver and its onReceive method looks like this:
@Override
public void onReceive(Context context, Intent intent) {
String value = intent.getExtras().getString("hiddenMessage");
MainActivity.getInstace().updateTheTextView(value);
}
Here's the code of the method which updates textView. This method is in MainActivity class:
public void updateTheTextView(final String textFromNotification) {
final Context context = this;
Toast toast = Toast.makeText(context, "Message received: " + textFromNotification, Toast.LENGTH_LONG);
toast.show();
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
textV1.setText(textFromNotification);
}
});
}
The problem is, when I send a notification from one app and receive it here, text is not visible. It's only visible when this app is open. How can I set text to field when my app is not open? Store message to some class field and use setText() in onResume method?
Upvotes: 0
Views: 920
Reputation: 1872
In your onReceive method after getting String value store it in sharedPreference like this
public void saveInSharedPreferences(String value){
SharedPreferences sharedpreferences = getSharedPreferences("sharedPref", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("variable",value);//your String value
editor.commit();
}
your onReceive method will be like
public void onReceive(Context context, Intent intent) {
String value = intent.getExtras().getString("hiddenMessage");
saveInSharedPreferences(value);
}
and in your Activity's onCreate method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String value = getValueFromSharedPreference();
if(value != null){
textview.setText(value);
}
}
getValueFromSharedPreference method
public String getValueFromSharedPreference(){
SharedPreferences sharedpreferences = getSharedPreferences("sharedPref", Context.MODE_PRIVATE);
return sharedpreferences.getString("variable",null);
}
Now if you want to update Textview when your app is open
Try this
public void onReceive(Context context, Intent intent) {
String value = intent.getExtras().getString("hiddenMessage");
saveInSharedPreferences(value);
Intent intent = new Intent("broadcast");
intent.putExtra("variable",value);
context.sendBroadCast(intent);
}
and then in your Activity's oncreate method
registerReceiver(broadcastReceiver, new IntentFilter("broadcast"));
and then in Your Activity's Class outside oncreate Method
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String value = intent.getStringExtra("variable");
textview.setText(value);
}
};
or you can even use this interface solution:https://stackoverflow.com/a/26268569/6799807
Upvotes: 1
Reputation: 16
First in onRecieve Method save the detail in sql
private void saveNotification(String msg) {
//notification_sql class to save detail
Notification_sql n=new Notification_sql(getApplicationContext());
Notification_detail notification_detail=new Notification_detail();
//notification detail model class to set and get get detail
notification_detail.setData(msg);
n.adddetail(notification_detail);
}
Second save detail in database
public String adddetail(Notification_detail dataval){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DATE_OF_ENTRY,dataval.getData());
// Inserting Row
db.insert(TABLE_NAME, null, values);
db.close(); // Closing database connection
return null;
}
Get data using task sheduler in Main Activity
scheduleTaskExecutor= Executors.newScheduledThreadPool(5);
// This schedule a task to run every 10 minutes:
scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
public void run() {
// If you need update UI, simply do this:
runOnUiThread(new Runnable() {
public void run() {
//update text view
Notification_sql meter_data=new Notification_sql(MapsActivity.this);
//getting detail from database which is saved just after message delivery
List<Notification_detail> contacts = meter_data.getDetail();
//System.out.println(contacts);
for (Notification_detail cn : contacts) {
String data=cn.getData();
txtdetail.setText(data)
}
}
});
}
}, 0, 10, TimeUnit.SECONDS);
Upvotes: 0