Android Service BroadcastReceiver And File Download

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.buzz.javatest" >

    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />

    <!-- Needed permissions in order to scan for beacons. -->
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

    <!-- 允许应用程序完全使用网络 -->
    <uses-permission android:name="android.permission.INTERNET" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <!-- Declaration that this app is usable on phones with Bluetooth Low Energy. -->
    <uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >

            <!--
            intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter
            -->
        </activity>

        <!-- Estimote service responsible for scanning beacons. -->
        <service android:name="com.estimote.sdk.service.BeaconService" android:exported="false" />
        <service android:name=".BackgroundService" android:exported="false" />

        <activity
            android:name=".SecondActivity"
            android:label="BuzzLib" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".DownloadActivity"
            android:label="BuzzLib" >
        </activity>

    </application>

</manifest>

BackgroundService

package com.example.buzz.javatest;

import android.app.Service;
import android.content.Intent;
import android.util.Log;
import android.os.*;
import android.widget.*;

import java.util.*;
import java.lang.*;
import java.io.Serializable;

import com.estimote.sdk.*;

/**
 * Created by NickChung on 21/01/2015.
 */
public class BackgroundService extends Service {

    private static final String TAG = BackgroundService.class.getSimpleName();

    public static final String EXTRAS_TARGET_ACTIVITY = "extrasTargetActivity";
    public static final String EXTRAS_BEACON = "extrasBeacon";

    private static final int REQUEST_ENABLE_BT = 1234;
    private static final Region ALL_ESTIMOTE_BEACONS_REGION = new Region("rid", null, null, null);

    private BeaconManager beaconManager;
    private Beacon beacon;
    private Region region;

    private Map<String, List<Integer>> dic;

    private int total;
    private int step = 6;

    @Override
    public void onCreate() {
        super.onCreate();

        dic = new HashMap<String, List<Integer>>();

        beaconManager = new BeaconManager(this);
        beaconManager.setBackgroundScanPeriod(500, 0);
        beaconManager.setForegroundScanPeriod(500, 0);

        beaconManager.setRangingListener(new BeaconManager.RangingListener() {
            @Override
            public void onBeaconsDiscovered(Region region, final List<Beacon> rangedBeacons) {
                for (Beacon beacon : rangedBeacons) {
                    int minor = beacon.getMinor();
                    int major = beacon.getMajor();
                    String uniqueKey = String.format("%s-%s", major, minor);

                    if (!dic.containsKey(uniqueKey)) {
                        List<Integer> rawList = new ArrayList<Integer>();
                        rawList.add(beacon.getRssi());
                        dic.put(uniqueKey, rawList);
                    } else {
                        List<Integer> rawList = dic.get(uniqueKey);
                        rawList.add(beacon.getRssi());
                    }
                }

                Map<String, Integer> seqMap = new HashMap<String, Integer>();
                for (Map.Entry<String, List<Integer>> entry : dic.entrySet()) {
                    total = 0;
                    List<Integer> rawList = entry.getValue();

                    if (rawList.size() > step) {
                        List<Integer> rawSubList = rawList.subList(rawList.size() - (step + 1), rawList.size() - 1);
                        for (int r : rawSubList) {
                            total += r;
                        }

                        int avgRssi = (int) (total / step);
                        seqMap.put(entry.getKey(), avgRssi);
                        //Log.i(TAG, String.format("%s:%s", entry.getKey(), avgRssi));
                    }
                }

                Intent intent = new Intent("android.intent.action.MY_BROADCAST");
                intent.putExtra("map",(Serializable)seqMap);
                sendBroadcast(intent);

            }
        });
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();

        if (!beaconManager.hasBluetooth()) {
            Toast.makeText(this, "Device does not have Bluetooth Low Energy", Toast.LENGTH_LONG).show();
        }

        if (!beaconManager.isBluetoothEnabled()) {
            Toast.makeText(this, "Bluetooth does not Enabled", Toast.LENGTH_LONG).show();
        } else {
            connectToService();
        }
        return super.onStartCommand(intent, flags, startId);
    }

    private void connectToService() {
        beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
            @Override
            public void onServiceReady() {
                try {
                    beaconManager.startRanging(ALL_ESTIMOTE_BEACONS_REGION);
                } catch (RemoteException e) {
                    Log.e(TAG, "Cannot start ranging", e);
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        try {
            beaconManager.stopRanging(ALL_ESTIMOTE_BEACONS_REGION);
        } catch (RemoteException e) {
            Log.d(TAG, "Error while stopping ranging", e);
        }

        beaconManager.disconnect();

        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }

}

SecondActivity

package com.example.buzz.javatest;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.content.IntentFilter;

import java.util.*;

public class SecondActivity extends ActionBarActivity {

    private BackgroundServiceReceiver backgroundServiceReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        backgroundServiceReceiver = new BackgroundServiceReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.intent.action.MY_BROADCAST");
        registerReceiver(backgroundServiceReceiver, intentFilter);

        Button btnGoThird = (Button) findViewById(R.id.btnGoThird);
        btnGoThird.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SecondActivity.this, DownloadActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_second, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = new Intent(this, BackgroundService.class);
        startService(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(backgroundServiceReceiver);
    }

    public class BackgroundServiceReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            TextView txtUI = (TextView) findViewById(R.id.txtUI);
            StringBuilder sb = new StringBuilder();

            HashMap<String, Integer> map = (HashMap<String, Integer>) intent.getSerializableExtra("map");

            for (Map.Entry<String, Integer> m : map.entrySet()) {
                sb.append(String.format("%s:%s,", m.getKey(), m.getValue()));
            }

            txtUI.setText(sb.toString());

        }

    }
}

HttpDownloader

package com.example.buzz.javatest;

/**
 * Created by NickChung on 3/5/15.
 */

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.util.Log;
import android.content.Context;

public class HttpDownloader {
    private URL url = null;

    /**
     * 根据URL下载文件,前提是文件当中的内容为文本,返回值就是文件当中的内容
     *
     * @param urlStr
     * @return
     */
    public String download(String urlStr) {

        StringBuffer buffer = new StringBuffer();
        String line = null;
        BufferedReader reader = null;

        try {
            url = new URL(urlStr);
            try {
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                while ((line = reader.readLine()) != null) {
                    buffer.append(line);
                }
            } catch (IOException e) {
                Log.e("io", "HttpURLConnection -> IOException");
                e.printStackTrace();
            }

        } catch (MalformedURLException e) {
            Log.e("url", "url -> MalformedURLException");
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return buffer.toString();

    }

    /**
     * 该函数返回整形: -1代表下载出错,0代表下载成功,1代表下载文件已存在
     *
     * @param urlStr
     * @param path
     * @param fileName
     * @return
     */
    public int download(Context context, String urlStr, String path, String fileName) {
        InputStream input = null;
        FileUtils fileUtils = new FileUtils();
        if (fileUtils.isFileExist(path + fileName)) {
            ((DownloadActivity) context).sendMsg(2, 0);
            return 1;
        } else {
            try {
                input = getInputStreamFromUrl(context, urlStr);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            File resultFile = fileUtils.writeToSDFromInput(context, path, fileName, input);
            if (resultFile == null) {
                return -1;
            }
        }
        return 0;
    }

    public InputStream getInputStreamFromUrl(Context context, String urlStr) throws IOException {
        url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        InputStream s = conn.getInputStream();

        ((DownloadActivity) context).sendMsg(0, conn.getContentLength());

        return s;
    }
}

FileUtils

package com.example.buzz.javatest;

/**
 * Created by NickChung on 3/4/15.
 */

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.os.Environment;

public class FileUtils {
    private String SDPATH;
    /**
     *
     */
    public FileUtils() {
        // TODO Auto-generated constructor stub
        //获得当前外部存储设备的目录
        SDPATH = Environment.getExternalStorageDirectory() + "/";
    }

    /**
     * 在SD卡上创建文件
     *
     * @param fileName
     * @return
     */
    public File createSdFile(String fileName) {
        File file = new File(SDPATH + fileName);
        try {
            file.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return file;
    }

    /**
     * 创建SD卡目录
     *
     * @param dirName
     * @return
     */
    public File createSDDir(String dirName) {
        File file = new File(SDPATH + dirName);
        file.mkdirs();

        return file;
    }

    public boolean isFileExist(String fileName) {
        File file = new File(SDPATH + fileName);

        return file.exists();

    }

    public File writeToSDFromInput(Context context, String path, String fileName, InputStream input) {
        File file = null;
        OutputStream output = null;

        try {
            createSDDir(path);
            file = createSdFile(path + fileName);
            output = new FileOutputStream(file);

            byte[] buffer = new byte[4 * 1024];
            int total = 0;
            while ((input.read(buffer)) != -1) {
                total = total + buffer.length;
                output.write(buffer);
                //更新下载进度条
                ((DownloadActivity) context).sendMsg(1, total);
            }
            output.flush();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                output.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //下载完成
        ((DownloadActivity) context).sendMsg(2, 0);
        return file;
    }

}

DownloadActivity

package com.example.buzz.javatest;

import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Looper;
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;
import android.app.ProgressDialog;
import android.os.Message;
import android.os.Handler;

import java.util.HashMap;
import java.util.Map;

public class DownloadActivity extends ActionBarActivity {

    private String TAG = this.getClass().getSimpleName();
    private BackgroundServiceReceiver backgroundServiceReceiver;
    private Button btnDownload;
    private ProgressDialog dialog = null;
    public static final int DIALOG_DOWNLOAD_PROGRESS = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);

        backgroundServiceReceiver = new BackgroundServiceReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.intent.action.MY_BROADCAST");
        registerReceiver(backgroundServiceReceiver, intentFilter);

        StrictMode.setThreadPolicy(
                new StrictMode
                        .ThreadPolicy
                        .Builder()
                        .detectDiskReads()
                        .detectDiskWrites()
                        .detectNetwork()
                        .penaltyLog()
                        .build());

        StrictMode.setVmPolicy(
                new StrictMode
                        .VmPolicy
                        .Builder()
                        .detectLeakedSqlLiteObjects()
                        .detectLeakedClosableObjects()
                        .penaltyLog()
                        .penaltyDeath()
                        .build());

        btnDownload = (Button) this.findViewById(R.id.btnDownload);
        btnDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开进度条
                showDialog(DIALOG_DOWNLOAD_PROGRESS);

                Thread t = new Thread(runnable);
                t.start();
            }
        });
    }

    Runnable runnable = new Runnable() {
        @Override
        public void run() {

            Looper.prepare();

            try {
                HttpDownloader httpDownloader = new HttpDownloader();
                int result = httpDownloader.download(DownloadActivity.this, "http://192.168.0.106:8080/exhibition/audio/a.mp3", "buzz/exhibition/audio/", "a.mp3");

                System.out.println(result);
                String str = null;
                if (result == 0) {
                    str = "下载完成";
                } else if (result == 1) {
                    str = "文件已经存在";
                } else {
                    str = "下载错误";
                }

                Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
            }
            catch(Exception ex) {
                Toast.makeText(getApplicationContext(), ex.toString(), Toast.LENGTH_LONG).show();
            }

            Looper.loop();
        }

    };

    public void sendMsg(int flag, int value) {
        Message message = new Message();
        message.what = flag;
        message.arg1 = value;
        handler.sendMessage(message);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_DOWNLOAD_PROGRESS:
                dialog = new ProgressDialog(this);
                dialog.setMessage("downloading…");
                dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                dialog.setCancelable(false);
                dialog.show();
                return dialog;
            default:
                return null;
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (!Thread.currentThread().isInterrupted()) {
                switch (msg.what) {
                    case 0:
                        dialog.setMax(msg.arg1);
                        break;
                    case 1:
                        dialog.setProgress(msg.arg1);
                        break;
                    case 2:
                        dialog.dismiss();
                        break;
                    case -1:
                        String error = msg.getData().getString("error");
                        Toast.makeText(DownloadActivity.this, error, Toast.LENGTH_LONG).show();
                        break;

                }
            }
            super.handleMessage(msg);
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_third, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(backgroundServiceReceiver);
    }

    public class BackgroundServiceReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            TextView txtTUI = (TextView) findViewById(R.id.txtTUI);
            StringBuilder sb = new StringBuilder();

            HashMap<String, Integer> map = (HashMap<String, Integer>) intent.getSerializableExtra("map");

            for (Map.Entry<String, Integer> m : map.entrySet()) {
                sb.append(String.format("%s:%s,", m.getKey(), m.getValue()));
            }

            txtTUI.setText(sb.toString());
        }
    }
}
时间: 2024-10-12 19:48:49

Android Service BroadcastReceiver And File Download的相关文章

android service broadcastreceiver intentfilter

service 组件,和UI并不进行交互,运行在后台,可以和其他组件进行交互 broadcastreceiver,广播是一种广泛运用的在应用程序之间传输信息的机制 .而 BroadcastReceiver 是对发送出来的广播进行过滤接收并响应的一类组件 intentfilter Intent类似于Windows中的消息,而intentfilter可以理解为消息过滤器 下面我们用例子来说明如何运用这三种组件 首先,我们建立一个Serivce,他的作用是每间隔一秒生成一个随机数,并将随机数放到Int

Android 利用Service BroadcastReceiver实现小例子

Activity: 1 package com.example.test; 2 3 import android.app.Activity; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.content.IntentFilter; 7 import android.os.Bundle; 8 import android.view.Menu; 9 import android.

java攻城狮之路(Android篇)--BroadcastReceiver&amp;Service

四大组件:activity 显示. contentProvider 对外暴露自己的数据给其他的应用程序.BroadcastReceiver 广播接收者,必须指定要接收的广播类型.必须明确的指定actionservice 服务,是运行后台,它是没有界面的.对某件事情进行监听. 一.广播:事件.普通广播: 是异步的.会广播接收者同时接收,不能被中断sendBroadcast()有序广播: 是同步的.会根据广播接收的优先级进行接收,是可以中断 短信到来广播 sendOrderBroadcast()-1

Android Service 服务(二)—— BroadcastReceiver

(转自:http://blog.csdn.net/ithomer/article/details/7365147) 一. BroadcastReceiver简介 BroadcastReceiver,用于异步接收广播Intent,广播Intent是通过调用Context.sendBroadcast()发送.BroadcastReceiver()接收. 广播Intent的发送是通过调用Context.sendBroadcast().Context.sendOrderedBroadcast().Con

Android service进程保护

应用进程保活基本就是围绕两个方面来展开: 1 尽量保证进程不被杀死. 2 进程被杀死后复活.细分如下: 1)Service重启 2)进程守护 3)Receiver触发 4) AlarmManager or JobScheduler循环触发 5)与系统Service捆绑-–可以不考虑,了解即可 下面将围绕这几点展开讨论. 一,基本概念 1.什么才叫应用进程保活 应用进程保活可以理解为应用位于后台永远不能被杀死.这里的可以简略地分为两种情况,第一种是当系统资源紧俏的时候或者基于某种系统自身的后台运行

Android Service演义

摘要: 本文基于Android 5.1代码,介绍了Android Service的运作机理.按理说,网上此类文章已经很多了,本不需我再赘述.但每个人理解技术的方式多少会有所不同,我多写一篇自己理解的service,也未尝不可吧. (本文以Android 5.1为准) 侯亮 1.概述 在Android平台上,那种持续性工作一般都是由service来执行的.不少初学者总是搞不清service和线程.进程之间的关系,这当然会影响到他们开展具体的开发工作. 其实,简单说起来,service和线程.进程是

Android Service与Activity之间通信的几种方式

在Android中,Activity主要负责前台页面的展示,Service主要负责需要长期运行的任务,所以在我们实际开发中,就会常常遇到Activity与Service之间的通信,我们一般在Activity中启动后台Service,通过Intent来启动,Intent中我们可以传递数据给Service,而当我们Service执行某些操作之后想要更新UI线程,我们应该怎么做呢?接下来我就介绍两种方式来实现Service与Activity之间的通信问题 通过Binder对象 当Activity通过调

【Android开发日记】初次探秘Android Service!Service开机启动+重力感应+弹窗+保持运行

前言: 最近在写一个小程序,需求是手机摇一摇就弹窗出来.第一次使用了Service,学习了两天,实现了Service弹窗,开机启动,Activity启动和销毁.满足了自己的需求.现记录学习心得.希望能给你带来一些帮助. 1.Service创建:重写4个方法 onBind():返回一个IBinder对象,这个对象可以使应用程序与Service通信.如果用startService.stopService启动和关闭Service的话,Service和访问者是无法通信交换数据的.onBind()返回值设

[Android随笔]BroadcastReceiver广播机制

一,介绍 android四大组件之一:BroadcastReceiver 翻译成中文:广播接收者.在Android中,Broadcast是一种广泛运用在应用程序之间传输信息的机制.而BroadcastReceiver是对发送出来的Broadcast(广播)进行过滤.接收.响应的一类组件. 呵呵,作为一个android研发学习者,我们想要学习使用BroadcastReceiver这个组件,当然实践是最好的方式.下面介绍BroadcastReceiver的几种使用方式. 二,使用方式 2.1 静态注