標籤:
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監聽網路連接狀態