• 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", "com.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());
            }
        }
    }
    
  • 相关阅读:
    NOIP200304麦森数
    NOIP201103瑞士轮
    NOIP200406合并果子
    Dijkstra堆优化与SPFA模板
    BZOJ1095: [ZJOI2007]Hide 捉迷藏
    NOIP201307货车运输
    hdu2062(递推)
    hdu1260(dp)
    hdu1158(dp)
    hdu1059(多重背包)
  • 原文地址:https://www.cnblogs.com/ncore/p/4315511.html
Copyright © 2020-2023  润新知