【问题标题】:How to run an application forever after installing? [duplicate]安装后如何永远运行应用程序? [复制]
【发布时间】:2018-09-26 09:34:01
【问题描述】:

我想在安装后在后台运行我的 android 应用程序。我尝试在后台运行应用程序。但是我必须在重启设备后自己启动应用程序。 我需要的是我需要将我的应用程序作为 FACEBOOK,whatsapp 工作。 据我所知,它们永远在后台运行,并且每次重新启动后都不需要手动重新启动。有人帮我吗?

【问题讨论】:

  • 只需实现android.intent.action.BOOT_COMPLETED广播接收器
  • 启动完成侦听器android.intent.action.BOOT_COMPLETED 可以解决问题。
  • 检查this
  • 这可以帮助你...Go here

标签: java android android-background


【解决方案1】:

在 AndroidManifest.xml 中

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

创建以 BootUpReceiver 命名的 Java 文件

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

【讨论】:

    【解决方案2】:

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

    您只需要从 Firebase 服务器发送参数,然后在类的 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 url 发出一个 Post 请求,并在 Json 对象中发送数据,其中包含以下数据,其中访问令牌是访问令牌

    https://fcm.googleapis.com/fcm/send
    Content-Type:application/json
    Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
    
    { "data": {
    "count": 5
    },
      "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-03
      • 2018-04-07
      相关资源
      最近更新 更多