zoukankan      html  css  js  c++  java
  • android 使用brocastReceiver监听网络连接状态

    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();
        }
    
        
    }
  • 相关阅读:
    Win10设置多时区时钟方法技巧
    Win10技巧:使用“照片”应用剪辑视频、添加特效
    kk录像机怎么剪辑视频 kk录像机视频剪辑教程
    360快剪辑怎么使用 360快剪辑软件使用方法介绍
    WPF HyperLink链接下划线隐藏
    ORACLE 如何产生一个随机数
    电脑的开始菜单点不了 用户帐户出现在桌面上
    无法加载DLL"***.dll":找不到指定的模块
    C#调用dll提示"试图加载格式不正确的程序"原因及解决方法
    C#中与C++中的 LPWSTR(wchar_t *) 对应的类型
  • 原文地址:https://www.cnblogs.com/lliuzl/p/4865018.html
Copyright © 2011-2022 走看看