如何将数据发送到另一个未启动的应用程序

How to send data to another app which is not started

我制作了一个许可证在 2 年后到期的应用程序。我正在制作另一个应用程序,我想用它来更新第一个应用程序的许可证。我将为用户提供一个密钥,他将在第二个应用程序中输入该密钥以更新第一个应用程序。为此,我想知道如何向我的第一个应用程序发送密钥?

您可以使用 BroadcastReceivers 在应用程序之间进行通信。 在此处查看文档:

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

在发送应用中:

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

在接收应用中:

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
   }

}

在接收应用的清单文件中,<application> 标签下:

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