2017-10-11 26 views
-2

的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,而不使用ContentProviderSharedPreference。此外,應用程序B應該接收其'BroadcastReceiver類中的值。任何人都可以幫助我嗎?

我已經從appB添加了appA MainActivity代碼和Receiver類和Manifest。

+0

利用Bluetoot API。 –

+1

在AppA中調用sendBroadcast()'請參閱[文檔](https://developer.android.com/guide/components/broadcasts.html)。 – CommonsWare

+0

如果我使用藍牙API,那麼我應該將appA和appB保存在單獨的設備中,但我想要的是 - appA和appB都將位於同一設備上,並且可以將數據從appA發送到appB的接收器類。 – Reyansh

回答

0

您可以使用sendBroadcast()方法將String值從一個應用程序發送到另一個應用程序。

但是,首先您需要在Manifest中註冊您的接收器應用程序。

例如,假設您有兩個應用程序,App1App2

App1將發送字符串值和App2將接收該值。

在你應用1,代碼應該是這樣的,

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

應用2收到這個廣播消息,在您的應用2Manifest寫代碼如下如下:

<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標誌被添加到inten t之前是 發送,以指示意圖被允許啓動已停止的應用程序的組件 。

+0

嗨馬赫迪,我已經做了完全如何你說,但應用程序B無法獲得我從AppA傳遞的價值, – Reyansh

+0

我已經添加了我所有的編碼,請建議我在哪裏滯後...謝謝 – Reyansh

+0

我已添加我的在這裏編碼以便更好地理解,請指導我在哪裏出錯。謝謝。 – Reyansh

相關問題