Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在不使用ContentProvider或SharedReference的情况下从其他应用程序向任何特定应用程序发送字符串值_Java_Android - Fatal编程技术网

Java 如何在不使用ContentProvider或SharedReference的情况下从其他应用程序向任何特定应用程序发送字符串值

Java 如何在不使用ContentProvider或SharedReference的情况下从其他应用程序向任何特定应用程序发送字符串值,java,android,Java,Android,appA主要活动: public class MainActivity extends AppCompatActivity implements View.OnClickListener { Button btnSendBroadcast; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); se

appA主要活动:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button btnSendBroadcast;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnSendBroadcast = (Button) findViewById(R.id.btnSendBroadcast);
        btnSendBroadcast.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        final Intent intent=new Intent();
        intent.setAction("com.example.admin.chromium");
        intent.putExtra("KeyName","code1id");
        intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        intent.setComponent(
                new ComponentName("com.example.admin.chromiumsendmessage","com.example.admin.chromiumsendmessage.MainActivity"));
        sendBroadcast(intent);
        Toast.makeText(getApplicationContext(), "KeyName value sent to ChromiumSendMessage app" , Toast.LENGTH_SHORT).show();

    }
}
appB清单--

有两个应用程序,appA和appB。在appA中有一个按钮。在appA中,如果我单击按钮,它将向appB发送一些值(
String/Integer
),而不使用
ContentProvider
SharedReference
)。此外,appB应该接收其'
BroadcastReceiver
类中的值。谁能帮我一下吗


我已经从appB中添加了appA MainActivity代码和Receiver类以及清单。

您可以使用
sendBroadcast()
方法将
字符串
值从一个应用发送到另一个应用

然而,首先你需要在
清单中注册你的接收者应用程序

例如,假设您有两个应用程序,App1App2

App1将发送字符串值,App2将接收该值

在你的App1中,代码应该是这样的

final Intent intent=new Intent();
intent.setAction("com.pkg.App1");
intent.putExtra("KeyName","code1id");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.setComponent(  
    new ComponentName("com.pkg.App2","com.pkg.App2.MyBroadcastReceiver"));  
sendBroadcast(intent);
要在App2中接收此广播消息,请在App2
Manifest
中编写以下代码:

<receiver android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
    <intent-filter>
      <action android:name="com.pkg.App1" />
    </intent-filter>
</receiver>

FLAG_INCLUDE_STOPPED_PACKAGES标志添加到意图之前 发送以指示允许启动组件的意图 停止的应用程序的


使用Bluetoot API。在应用程序A中调用
sendBroadcast()
。请参阅。如果我使用Bluetooth API,那么我应该将appA和appB都保存在单独的设备中,但我想要的是——appA和appB将位于同一设备上,并且可以将数据从appA发送到appB的接收器类。嗨,Mahedi,我完全按照你说的做了,但是appB无法从appA获得我传递的值,我已经添加了上面所有的编码,请建议我落后的地方…谢谢我在这里添加了我的编码,以便你更好地理解,请指导我哪里出了错。。谢谢。那么,将您的appA更改为intent.setComponent(新组件名(“com.example.admin.chromiumsendmessage”、“com.example.admin.chromiumsendmessage.MyBroadcast”);您好,非常感谢,在我将intent.setpackage(com.pkg.App2)放入aap2清单之后,它就工作了
final Intent intent=new Intent();
intent.setAction("com.pkg.App1");
intent.putExtra("KeyName","code1id");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
intent.setComponent(  
    new ComponentName("com.pkg.App2","com.pkg.App2.MyBroadcastReceiver"));  
sendBroadcast(intent);
<receiver android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
    <intent-filter>
      <action android:name="com.pkg.App1" />
    </intent-filter>
</receiver>