android 使用brocastReceiver監聽網路連接狀態

來源:互聯網
上載者:User

標籤:

public class BroadcastService extends Service{    private ConnectivityManager connectivityManager;//網路連接管理器    private NetworkInfo networkInfo;//當前網路的資訊    //點擊查看    private PendingIntent messagePendingIntent = null;      //通知欄訊息    private Notification messageNotification = null;    private NotificationManager messageNotificatioManager = null;    private Intent notificationIntent=null;    @Override    public IBinder onBind(Intent intent) {        // TODO Auto-generated method stub        return null;    }    /**     * 定義一個broadcastReceiver     */    private BroadcastReceiver myReceiver=new BroadcastReceiver() {                @Override        public void onReceive(Context context, Intent intent) {            // TODO Auto-generated method stub            String action=intent.getAction();            if(action.equals(ConnectivityManager.CONNECTIVITY_ACTION)){                connectivityManager=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);                WifiManager wifiManager=(WifiManager)context.getSystemService(Context.WIFI_SERVICE);                wifiManager.setWifiEnabled(true);                wifiManager.startScan();//開始掃描                List<ScanResult> listResults=wifiManager.getScanResults();                //listResults.get(0).toString();                networkInfo=connectivityManager.getActiveNetworkInfo();                if(networkInfo!=null&&networkInfo.isAvailable()){                    Log.d("netWorkInfo","當前網路連接:"+networkInfo.getTypeName());                }                else {                    Log.d("netWorkInfo","當前沒有網路連接");                }                            }        }    };    @Override    public void onCreate() {        // TODO Auto-generated method stub        super.onCreate();        IntentFilter filter=new IntentFilter();//過濾intent        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);//添加action        registerReceiver(myReceiver, filter);//註冊receiver        Log.i("tag","onCreate");    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        // TODO Auto-generated method stub        Log.i("tag","onStartCommand");        setNotification("新通知");        /*int intGetTastCounter=30;        ActivityManager mActivityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);        ArrayList<String> arylistTask = new ArrayList<String>();        List<ActivityManager.RunningTaskInfo> mRunningTasks = mActivityManager.getRunningTasks(intGetTastCounter);        int i = 0;         以迴圈及baseActivity方式取得工作名稱與ID          for (ActivityManager.RunningTaskInfo amTask : mRunningTasks)        {                     baseActivity.getClassName取出運行工作名稱                     arylistTask.add("" + (i++) + ": "+                     amTask.baseActivity.getClassName()+                    "(ID=" + amTask.id +")");                    Log.d("task", arylistTask.get(i-1));        }*/        return super.onStartCommand(intent, flags, startId);    }    /**     * 設定通知訊息     * @param message訊息類型     */    private void setNotification(String message){        notificationIntent = new Intent(this, SecondActivity.class);        messagePendingIntent = PendingIntent.getActivity(this, 0,notificationIntent,0);        messageNotificatioManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);        messageNotification=new Notification(R.drawable.ic_launcher, message, System.currentTimeMillis());        //messageNotification.contentIntent=messagePendingIntent;        messageNotification.setLatestEventInfo(this, message, "dfdf", messagePendingIntent);        // 顯示通知       messageNotificatioManager.notify("df",R.drawable.ic_launcher, messageNotification);    }    @Override    public void onDestroy() {        // TODO Auto-generated method stub                System.out.println("關閉服務了");        unregisterReceiver(myReceiver);//移除註冊的receiver        System.exit(0);        super.onDestroy();    }    }

 

android 使用brocastReceiver監聽網路連接狀態

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.