的appA mainActivity:我怎麼能一個字符串值發送到從其他應用程序的任何特定的應用程序,而無需使用的ContentProvider或sharedPreference
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的manifest--
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admin.chromiumsendmessage">
<uses-permission android:name="android.permission.SEND_SMS" />
<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=".MyBroadcast"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.admin.chromium" />
</intent-filter>
</receiver>
</application>
</manifest>
appB的接收器類:
public class MyBroadcast extends BroadcastReceiver {
String msg;
String name;
@Override
public void onReceive(Context context, Intent intent) {
msg = intent.getStringExtra("KeyName");
name= msg;
Toast.makeText(context, "value - " + name, Toast.LENGTH_SHORT).show();
}
}
AppB manifestappA mainActivity
有兩個應用程序,appA & appB。在appA中有一個按鈕。在appA中,如果我點擊按鈕,它應該發送一些值(String/Integer
)到appB,而不使用ContentProvider
或SharedPreference
。此外,應用程序B應該接收其'BroadcastReceiver
類中的值。任何人都可以幫助我嗎?
我已經從appB添加了appA MainActivity代碼和Receiver類和Manifest。
利用Bluetoot API。 –
在AppA中調用sendBroadcast()'請參閱[文檔](https://developer.android.com/guide/components/broadcasts.html)。 – CommonsWare
如果我使用藍牙API,那麼我應該將appA和appB保存在單獨的設備中,但我想要的是 - appA和appB都將位於同一設備上,並且可以將數據從appA發送到appB的接收器類。 – Reyansh