2017-01-10 92 views
-4

在我的应用程序中,我采取了两个文本字段,如picture。一个像“mr.x”这样的名称的文本字段就像生日一样。Android编程广播接收器

我保存数据名称和使用广播接收器通过下面的代码目的:

public void saveinfo(View view) { 
     SharedPreferences sp = getSharedPreferences("alarmatspecifictime", Context.MODE_PRIVATE); 
     SharedPreferences.Editor editor = sp.edit(); 
     editor.putString("name", nameinput.getText().toString()); 
     editor.putString("purpose",spinner.getSelectedItem().toString()); 
     editor.apply(); 
     makeText(this, "saved", LENGTH_LONG).show(); 

    } 

现在我想用从广播接收器类像"Today is mr.x Birthday"所保存的数据的通知消息。我该怎么做?

+0

以及究竟是你的问题?不清楚。 –

+0

我已经采取了两个文本字段之一是名称和另一个目的是为了目的我采取了一些spineer数据我保存名称和目的在我的sharedpreference.now我想从broadcastreceiver类retrud数据,并希望使用它在这里。 setContentText()。在通知类。 – rabiul

回答

0

我想你是唯一缺少的一小片:

String restoredText; 
String restoredname; 

public void addNotification(Context context) { 
    getPref(context); 
    String myString = restoredText + " " + restoredname; 
    ... 
    .setContentText(myString) 
    ... 
} 

public void getPref(Context context) { 
    restoredText = sp.getString("purpose", ""); 
    restoredname = sp.getString("name", ""); 
} 
0

你究竟想在这里做什么,这是非常不清楚的,而且你分享的代码没有直接意义。

如果你使用微调,那么可能有更多的一个项目,所以可能是问题是你正在使用共享首选项将它们保存在“目的”和“名称”键中的所有项目。

Sharedpreferences会覆盖何时插入了具有相同密钥的数据。所以每次使用sp.putString(“name”,);以前的值将被覆盖,并且只有新的值。

要得到更好,更正确的答案,请再次将您的查询语句改为短语。