Android Notification下载实例
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btn_start_service" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="start service" /> </LinearLayout
activity:
public class MainActivity extends Activity {
private Button btn_start_service; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); btn_start_service=(Button)findViewById(R.id.btn_start_service); btn_start_service.setOnClickListener(clickListener); } private OnClickListener clickListener=new OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this,StatusService.class); startService(intent); } }; @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); manager.cancel(R.layout.main);// 下载完成后,点击后,移除NotificationManager } }
services:
public class StatusService extends IntentService { private static final String TAG = "StatusService"; // private static final int KUKA = 0; public StatusService() { super("StatusService"); } @Override protected void onHandleIntent(Intent intent) { Log.i(TAG, "开始下载...."); showNotification(false);//开始下载 try { Thread.sleep(10000); showNotification(true);//下载完成 } catch (InterruptedException e) { e.printStackTrace(); } Log.i(TAG, "程序下载完毕"); } // finish下载是否完成 private void showNotification(boolean finish) { Notification notification; NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Intent intent = new Intent(this, MainActivity.class);//下拉点击执行的activity PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); if (!finish) { //图标 下载提示 时间 notification = new Notification(R.drawable.head, "开始下载", System.currentTimeMillis()); //显示在拉伸状态栏中的Notification属性,点击后将发送PendingIntent对象。 notification.setLatestEventInfo(this, "下载", "正在下载中", contentIntent); } else { notification = new Notification(R.drawable.head, "下载完毕", System.currentTimeMillis()); notification.setLatestEventInfo(this, "下载", "程序下载完毕", contentIntent); } //下载的默认声音 notification.defaults= Notification.DEFAULT_SOUND; //r.layout.main是为manager指定一个唯一的id manager.notify(R.layout.main, notification);//将自定义的notification放入NotificationManager } }
最后别忘记注册service
<service android:name=".StatusService"/>
更多详细介绍:
http://blog.csdn.net/qinjuning/article/details/6915482
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28