How to send data to another app which is not started

江枫思渺然 提交于 2020-02-16 06:23:50

问题


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>


来源:https://stackoverflow.com/questions/28198686/how-to-send-data-to-another-app-which-is-not-started

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!