Android 之 Notification通知栏

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值