Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我可以从启动启动一个活动并让它进入后台,而不让用户看到它吗,android_Java_Android_Service_Broadcastreceiver - Fatal编程技术网

Java 我可以从启动启动一个活动并让它进入后台,而不让用户看到它吗,android

Java 我可以从启动启动一个活动并让它进入后台,而不让用户看到它吗,android,java,android,service,broadcastreceiver,Java,Android,Service,Broadcastreceiver,目前,我有一些代码,可以从启动时启动应用程序,但可以将其打开到前台。 这是由 public void onReceive(Context context, Intent intent) { if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { Intent start = new Intent(context, ApolloMobileActivity.class);

目前,我有一些代码,可以从启动时启动应用程序,但可以将其打开到前台。 这是由

 public void onReceive(Context context, Intent intent) {
    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
        Intent start = new Intent(context, ApolloMobileActivity.class);  
        start.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(start);
然后,为了在启动时将其发送到后台,我创建了另一个名为StartAtBootService的java文件 因此,我将receiver类更改为:

if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
  Intent i = new Intent();
  i.setAction("com.example.ssab.StartAtBootService");
  context.startService(i);
                    }
服务类是

 public class StartAtBootService extends Service 
 {
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    @Override
    public void onCreate() 
    {
        Log.v("StartServiceAtBoot", "StartAtBootService Created");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) 
    {
        Log.v("StartServiceAtBoot", "StartAtBootService -- onStartCommand()");          

        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        return START_STICKY;
    }

    /*
     * In Android 2.0 and later, onStart() is depreciated.  Use
     * onStartCommand() instead, or compile against API Level 5 and
     * use both.
     * http://android-developers.blogspot.com/2010/02/service-api-changes-starting-with.html
        @Override
        public void onStart(Intent intent, int startId)
        {
            Log.v("StartServiceAtBoot", "StartAtBootService -- onStart()");         
        }
     */

    @Override
    public void onDestroy() 
    {
        Log.v("StartServiceAtBoot", "StartAtBootService Destroyed");
    }
 }
是否可以将StartAtBootService更改为在后台运行另一个名为ApolloMobileActivity的java文件中的活动? 我已经测试了这段代码,尽管它在启动时在后台运行,但它并没有在ApolloMobileActivity中运行这段代码

请帮忙!谢谢大家:)

活动是一个应用程序组件,它提供一个带有 哪些用户可以交互以执行某些操作,例如拨打电话 打电话、拍照、发送电子邮件或查看地图每个活动都是 给定要在其中绘制其用户界面的窗口。窗户 通常填充屏幕,但可能小于屏幕和 浮动在其他窗口的顶部


你可以开始一项活动,但不存在看不见的活动。这些是UI组件。如果你想在后台做看不见的工作,你必须在你的服务中做。

谢谢你的帮助,但是你能澄清一下如何使用服务对其进行编码吗?基本上只需将你想做的一切都放在
onStartCommand()
中,完成后,使用
service.stopSelf()
终止服务(出于性能/电池方面的原因)。请记住,服务在UI线程中运行,因此如果您必须执行繁重的工作(如计算、下载等),请使用
AsyncTasks
将您的工作从UI线程中移出。如果有帮助,请随时将此标记为正确答案。:)