如何在安装后永久运行一个应用程序?

How to run an application forever after installing?

我想在安装后 运行 我的 android 应用程序在后台运行。我试过 运行 在后台运行应用程序。但是我必须在重启设备后自己启动应用程序。 我需要的是我需要像 FACEBOOK、whatsapp 那样运行我的应用程序。 据我所知,它们 运行 永远在后台运行,不需要重新启动每次重启后手动。有人可以帮助我吗?

在AndroidManifest.xml

<receiver android:name=".BootUpReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

创建 Java 以 BootUpReceiver 命名的文件

public class BootUpReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
         if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
              //Do your coding here...
         }
    }
}

您可以使用 FirebaseMessagingService class 并使用它来执行您想要在其中执行的任务,它还可以用于执行其他操作,而不是创建推送通知,因为它始终在后台运行,您可以在服务器端配置代码以执行多项任务我使用 FirebaseMessaging 服务 class 执行应用程序的各种后台任务 一旦启动,它就会始终保留在后台,因为它是为发送推送通知而构建的,并且它们可以随时出现。

您只需要从 Firebase 服务器发送参数,并在 class

中的 onMessageReceived 方法中根据接收到的参数编写代码
public class NotificationService extends FirebaseMessagingService { 
  @Override
  public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
   int userCount=remoteMessage.getData("count");
//do anything using the parameters which are in remote message
       }
    }

您需要向 firebase 发出 Post 请求 url 并在 Json 对象内发送数据,其中包含以下数据 访问令牌在哪里

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

{ "data": {
"count": 5
},
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}