<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ethan.notification"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Notification4Activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".StatusService"></service>
</application>
</manifest>
Service类:
package com.ethan.notification;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.util.Log;
//IntentService异步交互
public class StatusService extends IntentService {
private static final String TAG = "StatusService";
public StatusService() {
super("StatusService");
}
public StatusService(String name) {
super("StatusService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent arg0) {
// TODO Auto-generated method stub
Log.i(TAG,"开始下载...");
showNotification(false);
try {
Thread.sleep(10000);
showNotification(true);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void showNotification(boolean finish) {
// TODO Auto-generated method stub
Notification notification = null;
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(this,Notification4Activity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if(!finish) {
notification = new Notification(R.drawable.ic_launcher,"开始下载",System.currentTimeMillis());
notification.setLatestEventInfo(this, "download", "downloading...", pi);
} else {
notification = new Notification(R.drawable.ic_launcher,"下载完毕",System.currentTimeMillis());
notification.setLatestEventInfo(this, "download", "下载完成!!!", pi);
nm.cancel(R.string.hello);
}
//R. 一个公共的ID int 数值
nm.notify(R.string.hello, notification);
}
}
Activity类:
package com.ethan.notification;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/*
* 下拉通知栏,提示下载完成,点击则回到原Activity
*/
public class Notification4Activity extends Activity {
/** Called when the activity is first created. */
private Button btnStartService;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnStartService = (Button) findViewById(R.id.button1);
btnStartService.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Notification4Activity.this,StatusService.class);
startService(intent);
}
});
}
protected void onStart() {
super.onStart();
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.cancel(R.string.hello);
}
}