首页 > 代码库 > Android 后台每10秒钟启动一次应用的demo,一次启动,永不退出

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,一次启动,永不退出