簡體   English   中英

廣播接收器 - 在應用程序之間發送字符串

[英]Broadcast receiver - send String between apps

我正在嘗試將字符串從應用程序發送到應用程序。

第一個名為“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();
            }
        }
    });
}

第二個名為“rec”的應用程序有兩個類“broadcastReciver”和“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"));
    }
}

廣播接收器:

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

    }
}

我還在“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>

應用程序應該做的是,當我在第一個應用程序中輸入內容並通過按鈕向它發送另一個按鈕時,它應該在第二個應用程序中“廣播”(顯示)吐司。

我的第二個應用程序在運行時沒有顯示任何數據。

如今,必須在廣播接收器的意圖過濾器中指定操作。

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

發送廣播時,您需要為發送的意圖設置完全相同的操作。

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

操作名稱的符號對於讓您的代碼正常工作可能不是很重要,但我建議提供與您的發送應用程序包相關的名稱。 例如: "com.username.example.myApplication.ACTION_EXAMPLE"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM