android的Service和Notification学习
在安卓应用中经常看到比如一连接网络 某个应用就在状态栏上更新一些信息
今天花了一个下午研究下贴上代码:
package
com.example.study;
import
android.app.Activity;
import
android.content.Context;
import
android.content.Intent;
import
android.net.ConnectivityManager;
import
android.net.NetworkInfo;
import
android.os.Bundle;
import
android.view.Menu;
import
android.view.View;
import
android.view.View.OnClickListener;
public
class
MainActivity
extends
Activity
implements
OnClickListener {
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String data = intent.getStringExtra(
"data"
);
if
(data !=
null
&& data.equals(
"data"
)
&& ServiceDemo.notificationManager !=
null
) {
ServiceDemo.notificationManager.cancel(
0
);
ServiceDemo.notificationManager=
null
;
}
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return
true
;
}
public
boolean
detect(Context con) {
ConnectivityManager manager = (ConnectivityManager) con
.getApplicationContext().getSystemService(
Context.CONNECTIVITY_SERVICE);
if
(manager ==
null
) {
return
false
;
}
NetworkInfo networkinfo = manager.getActiveNetworkInfo();
if
(networkinfo ==
null
|| !networkinfo.isAvailable()) {
return
false
;
}
return
true
;
}
@Override
public
void
onClick(View v) {
switch
(v.getId()) {
case
R.id.start_service:
this
.startService(
new
Intent(
this
, ServiceDemo.
class
));
break
;
default
:
break
;
}
}
}
|
service代码:
package com.example.study; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.util.Log; public class ServiceDemo extends Service { public static NotificationManager notificationManager; Handler mHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 1: if (notificationManager == null) { addNotificaction(); } break; default: break; } } }; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); new Thread(new Runnable() { @Override public void run() { while (true) { try { Thread.sleep(1000 * 60); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } Boolean b = detect(ServiceDemo.this); System.out.println(b); if (b) { Message msg = new Message(); msg.what = 1; mHandler.sendMessage(msg); } } } }).start(); } private void addNotificaction() { Notification notification = new Notification(R.drawable.ic_launcher, "Notification测试", System.currentTimeMillis()); notificationManager = (NotificationManager) this .getSystemService(Context.NOTIFICATION_SERVICE); Intent startintent = new Intent(this, MainActivity.class); startintent.putExtra("data", "data"); PendingIntent pendingIntent02 = PendingIntent.getActivity(this, 0, startintent, 0); notification.setLatestEventInfo(this, "测试", "跳转到主界面", pendingIntent02); notificationManager.notify(0, notification); } public boolean detect(Context con) { ConnectivityManager manager = (ConnectivityManager) con .getApplicationContext().getSystemService( Context.CONNECTIVITY_SERVICE); if (manager == null) { return false; } NetworkInfo networkinfo = manager.getActiveNetworkInfo(); if (networkinfo == null || !networkinfo.isAvailable()) { return false; } return true; } @Override public void onDestroy() { super.onDestroy(); Log.v("CountService", "on destroy"); } } 代码下载:http://download.csdn.net/detail/wenwei19861106/4566084