service相关

在service中注册广播

package com.Robyn.upgrade2;

import android.app.DownloadManager;
import android.app.DownloadManager.Request;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Environment;
import android.os.IBinder;

/**
 * 检测安装更新文件的助手类
 * 
 * @author G.Y.Y
 * 
 */ 

public class UpdataService extends Service { 

    /** 安卓系统下载类 **/ 
    DownloadManager manager; 

    /** 接收下载完的广播 **/ 
    DownloadCompleteReceiver receiver; 

    /** 初始化下载器 **/ 
    private void initDownManager() { 

        manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); 

        receiver = new DownloadCompleteReceiver(); 

        //设置下载地址 
        DownloadManager.Request down = new DownloadManager.Request( 
                Uri.parse("http://wlkcms.res.solutioncenter.top/upgrade_wwl_android_apk/sdcard/Upgrade.apk")); 

        // 设置允许使用的网络类型,这里是移动网络和wifi都可以 
        down.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE 
                | DownloadManager.Request.NETWORK_WIFI); 

        // 下载时,通知栏显示途中 
        down.setNotificationVisibility(Request.VISIBILITY_VISIBLE); 

        // 显示下载界面 
        down.setVisibleInDownloadsUi(true); 

        // 设置下载后文件存放的位置 
        down.setDestinationInExternalFilesDir(this, 
                Environment.DIRECTORY_DOWNLOADS, "Upgrade.apk"); 

        // 将下载请求放入队列 
        manager.enqueue(down); 

        System.out.println("kaishixiazai");

        //注册下载广播 
        registerReceiver(receiver, new IntentFilter( 
                DownloadManager.ACTION_DOWNLOAD_COMPLETE)); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

        // 调用下载 
        initDownManager(); 

        return super.onStartCommand(intent, flags, startId); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 

        return null; 
    } 

    @Override 
    public void onDestroy() { 

        // 注销下载广播 
        if (receiver != null) 
            unregisterReceiver(receiver); 

        super.onDestroy(); 
    } 

    // 接受下载完成后的intent 
    class DownloadCompleteReceiver extends BroadcastReceiver { 

        @Override 
        public void onReceive(Context context, Intent intent) { 

            //判断是否下载完成的广播 
            if (intent.getAction().equals( 
                    DownloadManager.ACTION_DOWNLOAD_COMPLETE)) { 

                //获取下载的文件id 
                long downId = intent.getLongExtra( 
                        DownloadManager.EXTRA_DOWNLOAD_ID, -1); 

                //自动安装apk 
                installAPK(manager.getUriForDownloadedFile(downId)); 
                  System.out.println("kaishianzhuang");
                //停止服务并关闭广播 
                UpdataService.this.stopSelf(); 

            } 
        } 

        /**
         * 安装apk文件
         */ 
        private void installAPK(Uri apk) { 

            // 通过Intent安装APK文件 
            Intent intents = new Intent(); 

            intents.setAction("android.intent.action.VIEW"); 
            intents.addCategory("android.intent.category.DEFAULT"); 
            intents.setType("application/vnd.android.package-archive"); 
            intents.setData(apk); 
            intents.setDataAndType(apk,"application/vnd.android.package-archive"); 
            intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
            android.os.Process.killProcess(android.os.Process.myPid()); 
            // 如果不加上这句的话在apk安装完成之后点击单开会崩溃 
            System.out.println("kaishianzhuang2");
            startActivity(intents); 

        } 

    } 

一一一一一一一一一一一我是一条你看不见的分割线一一一一一一一一一


服务在onStartCommand中调用可暂时使服务后台运行
importandroid.app.Service;
importandroid.content.Intent;
importandroid.os.IBinder;

publicclassMyServerextendsService{
     
     @Override
     publicvoidonCreate() {
          
          super.onCreate();
     }
     
     private class ServiceThread extends Thread{

          @Override
          publicvoidrun() {
              //TODOAuto-generated method stub
              super.run();
              for(inti= 0;i< 100;i++) {
                   System.out.println(i);
                   try{
                        Thread.sleep(1000);
                   }catch(InterruptedExceptione) {
                        //TODOAuto-generated catch block
                        e.printStackTrace();
                   }
              }
              
          }
          
     }         
     @Override
     publicintonStartCommand(Intentintent,intflags,intstartId) {
          ServiceThreadst=newServiceThread();
          st.start();
          returnsuper.onStartCommand(intent,flags,startId);
     }
     @Override
     publicIBinder onBind(Intentarg0) {
          //TODOAuto-generated method stub
          returnnull;
     }
     

}


//主方法中调用启动服务
Intentintent=newIntent();
intent.setClass(MainActivity.this, MyServer.class);
startService(intent); 

一一一一一一一一一一一我是一条你看不见的分割线
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值