简体   繁体   English

如何通过android中的意图发送数据而不打开另一个活动?

[英]How to send data through intent in android without opening another activity?

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... 在这里我不想打开这个Searchbyitem.class只是发送数据...

Yes i have also faced this problem . 是的我也遇到过这个问题。

Many developers also facing problems when passing data from dialog to another activity through Intent or Bundle. 许多开发人员在通过Intent或Bundle将数据从对话框传递到另一个活动时也遇到了问题。 It returns null at the retrieving time from another activity. 它在检索时从另一个活动返回null。

And the only solution is SharedPreferences. 唯一的解决方案是SharedPreferences。

But you have to place it inside the dismiss button.( ex: ok/cancel etc) 但你必须将它放在解雇按钮内。(例如:确定/取消等)

And retrieve the data from another activity easily through the same key. 并通过相同的密钥轻松地从另一个活动中检索数据。 Do not use any service followed by broadcast intent . 请勿使用广播意图后面的任何服务。

The code in dialog activity is like this: 对话框活动中的代码如下:

    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();

    }

});

And from another fetch the data like this: 从另一个获取这样的数据:

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

Add some checkings for a good standard. 添加一些检查以获得良好的标准。

Thank you 谢谢

你也可以使用SharedPreferences来实现这一目标

You probably want to use a Service not an activity. 您可能希望使用Service而不是活动。

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

You can try EventBus or Otto Android libraries to communicate between activities, services and fragments.. 您可以尝试使用EventBusOtto Android库在活动,服务和片段之间进行通信。

So you should create a Service to pass data and for communication between activities, fragments etc use an event bus 因此,您应该创建一个服务来传递数据,并在活动,片段等之间进行通信,使用事件总线

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android中没有意图的情况下将数据从活动发送到另一个活动 - How to send data from activity to another activity without Intent in android 如何在不打开数据的情况下将数据发送到另一个活动 - How to send data to another activity without opening it 如何在Android中使用Intent将图片从一个活动发送到另一个活动 - How to send Images one Activity to another Activity using Intent in android 如何单击图像视图并通过意图在另一个活动中发送该图像? - How to click image view and send that image in another activity through intent? 如何通过意图将字符串值从一个活动发送到另一个活动? - How to send String values from one activity to another through intent? 如何通过 Intent 将对象发送到 Android 中的另一个 Activity? - How to send the object via intent to another Activity in Android? 如何使用for循环意图将多行数据发送到另一个活动? - How to send multiple rows of data to another activity in intent using for loop? 如何通过意图发送从另一个活动发送的数据? - How to send the data which is sent from another activity by intent? 不移动下一个活动从android中的另一个活动发送数据 - without moving next activity Send data from another activity in android 如何将 MutableMap 从活动发送到另一个有意图的活动 - How to send MutableMap from activity to another with intent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM