8

here is my code for sending data by intent but i don't want open another activity i just want to send the data without opening it..

Bundle contain = new Bundle();
            contain.putString("key4", item);
            contain.putString("price", price);

Intent a  = new Intent(Searchbydate.this, Searchbyitem.class);
            a.putExtras(contain);
            startActivity(a); 

here i don't want to open this Searchbyitem.class just send the data...

4

4 回答 4

5

是的,我也遇到过这个问题。

许多开发人员在通过 Intent 或 Bundle 将数据从对话框传递到另一个活动时也面临问题。它在从另一个活动中检索时返回 null。

唯一的解决方案是 SharedPreferences。

但是您必须将其放在关闭按钮内。(例如:确定/取消等)

并通过相同的密钥轻松地从另一个活动中检索数据。不要使用任何带有广播意图的服务。

对话活动中的代码是这样的:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setIcon(R.drawable.mailicon);
    builder.setTitle(name);
    builder.setView(view);

    builder.setPositiveButton("Send Request",new DialogInterface.OnClickListener()
    {

     @Override 
     public void onClick(DialogInterface dialog,    int which) {
     String mailID = id.getText().toString();

     //Here define all your sharedpreferences code with key and value
     SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_PRIVATE);
     SharedPreferences.Editor edit = prefs.edit();
     edit.putString("MID", mailID );
     edit.commit();

    }

});

并从另一个获取这样的数据:

    SharedPreferences bb = getSharedPreferences("my_prefs", 0);
    String m = bb.getString("NUM", "");
    Toast.makeText(this, m, Toast.LENGTH_SHORT).show();

添加一些检查以获得良好的标准。

谢谢

于 2013-08-02T09:43:02.503 回答
3

你打电话也SharedPreferences用来存档

于 2013-03-17T21:57:02.873 回答
1

您可能想使用Service非活动。

阅读:http: //developer.android.com/guide/components/fundamentals.html#Components

于 2013-03-17T21:56:07.943 回答
1

您可以尝试使用 EventBusOtto Android 库在活动、服务和片段之间进行通信。

所以你应该创建一个服务来传递数据和活动之间的通信,片段等使用事件总线

于 2013-03-17T21:57:18.223 回答