您的位置:首页 > 移动开发 > Android开发

【攻克Android (32)】Notification 通知

2015-08-17 19:57 651 查看
[size=large]本文围绕以下两个部分展开:[/size]

[size=large]一、通知[/size]
[size=large]案例一[/size]


[size=large]一、通知[/size]

[size=medium]1. 概念:[/size]

[size=medium]Notification提醒用户,这些提醒可以通过很多途经去引起用户的注意,如闪烁背景灯、设备震动、播放背景音乐等等。 一个典型的方法是在状态栏上放一个图标,用户可以打开它获取信息。[/size]

[size=medium]Notification是一些不可见组件(Broadcast Receivers、Services)通知用户的优先选择。[/size]

[size=medium]2. Notification与Toast的区别[/size]

[size=medium]Notification与Toast都可以起到通知、提醒的作用,但它们的实现原理和表现形式完全不一样。[/size]

[size=medium](1)Toast其实相当于一个组件(Widget),有些类似于没有按钮的对话框。而Notification是显示在屏幕上方状态栏中的信息。[/size]

[size=medium](2)Notification需要用NotificationManager来管理,而Toast只需要简单地创建Toast对象即可。[/size]

[size=medium]3. PendingIntent(挂起Intent)和Intent区别[/size]

[size=medium]Intent通常用于马上处理的事情,PendingIntent通常使用于未来处理的事情,常常用于Notification和AlarmManager。PendingIntent就是一个Intent的描述,我们可以把这个描述交给别的程序,别的程序根据这个描述在后面的别的时间做你安排做的事情。 [/size]

[size=medium]由于 Notification可以与应用程序脱离。也就是说,即使应用程序关闭,Notification仍然会显示在状态栏中。当应用程序启动后,又可以重新控制这些 Notification,如清除或替换它们。因此需要创建一个 PendingIntent对象。该对象由Android负责维护,因此,在应用程序关闭后,该对象仍然不会被释放。【比如,某个 app弹出通知,当app关掉后,通知还显示在通知栏中。】[/size]

[size=medium]Intent通常用于马上处理的事情,PendingIntent通常使用于未来处理的事情,常常用于Notification和AlarmManager。PendingIntent就是一个Intent的描述,我们可以把这个描述交给别的程序,别的程序根据这个描述在后面的别的时间做你安排做的事情。 [/size]


[size=large]案例一[/size]

[align=center][/align]

[align=center][/align]

[align=center][/align]

[align=center][/align]

[align=center][/align]


[size=medium]1. 创建一个 IntentService:NotificationService。[/size]

package com.android.notification;

import android.app.IntentService;
import android.content.Intent;

public class NotificationService extends IntentService {

public NotificationService() {
super("NotificationService");
}

@Override
protected void onHandleIntent(Intent intent) {

}
}


[size=medium]2. activity_main.xml。写一个按钮。[/size]

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<Button
android:id="@+id/btnDownload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="download"
android:text="下载" />

</RelativeLayout>


[size=medium]3. MainActivity。按钮点击事件。[/size]

/**
* 按钮点击事件
*
* @param view
*/
public void download(View view) {
// 下载的时候,启动通知服务
startService(new Intent(this, NotificationService.class));
}


[size=medium]4. NotificationService。写通知服务。[/size]

package com.android.notification;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

public class NotificationService extends IntentService {
private static final String TAG = "NotificationService";

public NotificationService() {
super("StatusService");
}

@Override
protected void onHandleIntent(Intent intent) {
Log.i(TAG, "开始下载...");
// 1. 设置正在下载(false)
showNotification(false);
// 2. 下载
try {
// 模拟下载 - 10秒
Thread.sleep(10000);
// 通知:下载完成
showNotification(true);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i(TAG, "下载完成...");
}

/**
* 3. 显示通知
*
* @param finish 下载完成为 true
*/
private void showNotification(boolean finish) {
// 4. 创建通知管理器(通知管理的服务)
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
// 5.
/*
NotificationCompat.Builder :
Builder class for NotificationCompat objects.
Allows easier control over all the flags,
as well as help constructing the typical notification layouts.
*/
NotificationCompat.Builder builder = null;

// 6. 设置跳转到某个活动的意图 (此程序中,是回到主界面)
Intent intent = new Intent(this, MainActivity.class);

// 7.
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);

// 8. 更新通知中的文字
// 如果下载未完成(开始下载)
if (!finish) {
builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.sym_call_missed)
.setContentTitle("开始下载")
.setContentText("正在下载中")
.setContentIntent(contentIntent);
} else {
builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.sym_call_missed)
.setContentTitle("下载完成")
.setContentText("下载完成")
.setContentIntent(contentIntent);

}

// 9. 建造 通知
Notification notification = builder.build();

// 10. 通知声音/震动/LED闪光
notification.defaults = Notification.DEFAULT_ALL;
// 11. 当点击 “下载” 按钮的时候,通知管理器 唤醒通知
nm.notify(R.id.btnDownload, notification);
}
}


[size=medium]5. MainActivity。当点击通知,重新回到活动界面(此时会调用 onStart 方法)后,通知就取消掉了。[/size]

/**
* 当点击通知,重新回到活动界面(此时会调用 onStart 方法)后,通知就取消掉了。
*/
@Override
protected void onStart() {
super.onStart();
// 16.1 创建通知管理器(通知管理的服务)
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
// 16.2 通知管理器 取消通知
// 注意:取消通知的 id 和 15.中 通知的 id 要一致
nm.cancel(R.id.btnDownload);
}



[size=medium]代码补充:[/size]

[size=medium]MainActivity。[/size]

package com.android.notification;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

/**
* 按钮点击事件
*
* @param view
*/
public void download(View view) {
// 下载的时候,启动通知服务
startService(new Intent(this, NotificationService.class));
}

/**
* 当点击通知,重新回到活动界面(此时会调用 onStart 方法)后,通知就取消掉了。
*/
@Override
protected void onStart() {
super.onStart();
// 16.1 创建通知管理器(通知管理的服务)
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
// 16.2 通知管理器 取消通知
// 注意:取消通知的 id 和 15.中 通知的 id 要一致
nm.cancel(R.id.btnDownload);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: