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();
}
}