How to send data to another app which is not start

2019-03-04 17:43发布

问题:

I have made a app for which licence expires after 2 years. And I am making another app which I want to use for renewal of the licence of the first app. I will provide user with a key which he will enter in second app to renew the first app. For this I want to know How do I send my first app the key?

回答1:

You can communicate between apps using BroadcastReceivers. Check the documentation here:

http://developer.android.com/reference/android/content/BroadcastReceiver.html

In the sending app:

public void broadcastIntent(View view)
{
   Intent intent = new Intent();
   intent.setAction("SOME_ACTION");
   intent.putExtras("key",key);
   sendBroadcast(intent);
}

In the receiving app:

public class MyReceiver extends BroadcastReceiver {

   @Override
   public void onReceive(Context context, Intent intent) {
      Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
      String key=intent.getStringExtra("key");
      // do something with the key
   }

}

In the manifest file of the receiving app, under <application> tag:

<receiver android:name="MyReceiver">
      <intent-filter>
         <action android:name="SOME_ACTION"> <!-- Here you can use custom actions as well, research a little -->
      </action>
      </intent-filter>
   </receiver>