Android 后台每10秒钟启动一次应用的demo,一次启动,永不退出

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 后台每10秒钟启动一次应用的demo,一次启动,永不退出相关的知识,希望对你有一定的参考价值。

android整理

原文链接

创建一个Service:

最关键的是onStartCommand 里面返回的Service.START_STICKY, 

package com.qywanwei.servicetest2;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.IBinder;
import android.support.v7.app.NotificationCompat;
import android.util.Log;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

import java.util.List;


public class MyService extends Service  implements  Runnable{
    private Thread thread;
    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    //创建服务时候调用,第一次创建
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("MyServer", "onCreate: 创建服务");
        //onCreate的时候创建初始化
        thread = new Thread( this);
        thread.start();
//        前台服务
//        Intent intent=new Intent(this,MainActivity.class);
//        PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);
//        Notification notification=new NotificationCompat.Builder(this)
//                .setContentTitle("111")
//                .setContentText("22")
//                .setWhen(System.currentTimeMillis())
//                .setSmallIcon(R.mipmap.ic_launcher)
//                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
//                .setContentIntent(pendingIntent)
//                .build();
//        startForeground(1,notification);

    }
    //每次服务启动调用,每次启动
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("MyServer", "onCreate: 启动服务");
        //如果服务并停止了,重新生成一个新的
        if(thread.isInterrupted()){
            thread = new Thread(this);
            thread.start();
        }
        return Service.START_STICKY;
//        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void run() {
        int i=0;
        while (true){
            try {
                //每10秒钟进行一次输出
                Thread.sleep(10000);
                //Toast.makeText(getApplicationContext(),"服务启动"+i++,Toast.LENGTH_LONG).show();
                Log.d("MyServer", "服务启动"+i++);
                openApp("com.qywanwei.servicetest2");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }


    //    private void openApp(View v, String packageName) {
    private void openApp(String packageName) {
        //Context context = v.getContext();
        PackageInfo pi = null;
        //PackageManager pm = context.getPackageManager();
        PackageManager pm = getPackageManager();
        try {
            pi = pm.getPackageInfo(packageName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        resolveIntent.setPackage(pi.packageName);

        List<ResolveInfo> apps = pm.queryIntentActivities(resolveIntent, 0);

        ResolveInfo ri = apps.iterator().next();
        if (ri != null ) {
            packageName = ri.activityInfo.packageName;
            String className = ri.activityInfo.name;

            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);

            ComponentName cn = new ComponentName(packageName, className);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setComponent(cn);
            startActivity(intent);
        }
    }

    //服务销毁的时候
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("MyServer", "onCreate: 销毁服务");
    }
}

  

以上是关于Android 后台每10秒钟启动一次应用的demo,一次启动,永不退出的主要内容,如果未能解决你的问题,请参考以下文章

在几秒钟内多次调用 Healthkit 后台交付

Android Flutter App 中的持久后台工作程序,每 30 秒获取一次 Web API

Xamarin Forms - 每 10 秒获取一次设备位置(当应用程序在前台/后台运行时)

如何从Ansible-playbook的bash-script启动后台循环

iOS WKWebView JS在后台应用程序时不更新属性修改

如何在 Android 中使用后台服务每 5 分钟获取一次设备位置?