简体   繁体   English

广播接收器 - 在应用程序之间发送字符串

[英]Broadcast receiver - send String between apps

I am trying to send string from app to app.我正在尝试将字符串从应用程序发送到应用程序。

First app called "send" has only "MainActivity" class and layout:第一个名为“send”的应用程序只有“MainActivity”类和布局:

private void sendMsg(){

    final TextView msg = (TextView) findViewById(R.id.sendText);
    Button snd = (Button)findViewById(R.id.sendButton);

            snd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(!msg.getText().toString().trim().equals("")){
                Intent intent = new Intent("Updated");
                intent.setAction(Intent.ACTION_SEND);
                intent.putExtra("TEXT", msg.getText().toString().trim());
                intent.setType("text/plain");
                intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                intent.setComponent(new ComponentName("com.example.rec","com.example.rec.broadcastReciver"));
                getApplicationContext().sendBroadcast(intent);
            }else{
                Toast.makeText(getApplicationContext(), "Write text that You want to broadcast!", Toast.LENGTH_LONG).show();
            }
        }
    });
}

Second app called "rec" has two classes "broadcastReciver" and "MainActivity".第二个名为“rec”的应用程序有两个类“broadcastReciver”和“MainActivity”。

MainActivity:主要活动:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        zoviBroadCast();
    }

    private void zoviBroadCast(){
        broadcastReciver brcv = new broadcastReciver();
        registerReceiver(brcv,
                new IntentFilter("action"));
    }
}

broadcastReciver:广播接收器:

public class broadcastReciver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent)
    {
        //String data = intent.getStringExtra("TEXT").trim();
        if (intent != null)
        {
            String sIntentAction = intent.getAction();
            if (sIntentAction != null && sIntentAction.equals("action"))
            {
                String data = intent.getStringExtra("TEXT").trim();
                Toast.makeText(context, data, Toast.LENGTH_LONG).show();
            }
            else {
                Toast.makeText(context,"Something went wrong",Toast.LENGTH_SHORT).show();
            }
        }

    }
}

I also added lines between tag "receiver" in "AndroidManifest.xml":我还在“AndroidManifest.xml”中的“receiver”标签之间添加了几行:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.rec">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver
            android:name=".broadcastReciver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="action" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

What application should do is when I type something in first application and send it another over button it should "broadcast" (show) toast at second app.应用程序应该做的是,当我在第一个应用程序中输入内容并通过按钮向它发送另一个按钮时,它应该在第二个应用程序中“广播”(显示)吐司。

My second application is not showing any data when run.我的第二个应用程序在运行时没有显示任何数据。

Nowadays it is essential to specify an action in intent filter of your broadcast receiver.如今,必须在广播接收器的意图过滤器中指定操作。

<receiver android:name="MyReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.MY_ACTION">          
            </action>
        </intent-filter>
    </receiver>

When sending the broadcast, you need to set exactly the same action to the intent you send.发送广播时,您需要为发送的意图设置完全相同的操作。

Intent i = new Intent();
i.setAction("android.intent.action.MY_ACTION");
context.sendBroadcast(i);

Notation of action name may be not very important to get your code working, but I recommend to give names related to the package of your sending app.操作名称的符号对于让您的代码正常工作可能不是很重要,但我建议提供与您的发送应用程序包相关的名称。 For example: "com.username.example.myApplication.ACTION_EXAMPLE"例如: "com.username.example.myApplication.ACTION_EXAMPLE"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM