通知栏的使用

通知栏有两种开启方式

一、通过NotifyManager

“`

    NotificationManger notificationManger=(NotificationManger)getSystemService(Context.NOTIFICAT_SERVCE);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    //自定义样式需要使用RemoteViews
    views = new RemoteViews(getPackageName(), R.layout.layout_download);
    builder.setContent(views);
    views.setProgressBar(R.id.pb, 100, 50, true);
    builder.setSmallIcon(R.mipmap.ic_logo);//需要设置通知栏的logo,注:在android5.0,应用目标版本在21之后的,logo图片只支持白色和透明色两种颜色,否则会显示白框。
    notification = builder.build();
    //发送一个通知
    notificationManger.notify(id,Notification);

二、在server中通过startForegroud

在服务中通过startForeground(1,notification)来抛出一个通知,可以用来提高进程的优先级。服务执行该方法后,通知在状态栏无法通过滑动关闭


        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        //自定义样式需要使用RemoteViews
        views = new RemoteViews(getPackageName(), R.layout.layout_download);
        builder.setContent(views);
        views.setProgressBar(R.id.pb, 100, 50, true);
        builder.setSmallIcon(R.mipmap.ic_logo);//需要设置通知栏的logo,注:在android5.0,应用目标版本在21之后的,logo图片只支持白色和透明色两种颜色,否则会显示白框。
        notification = builder.build();
        //开启前台通知
        startForeground(1000, notification);

通知栏事件设置

   Intent notificationIntent = new Intent(getApplicationContext(), DownloadReceiver.class);//这是发送一个广播
                notificationIntent.setAction(ACTION_REPTY);
                PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
                notification.contentIntent = contentIntent;
                notificationManger.notify(id,Notification);//可以通过再次发送同样id通知来更新通知的内容或者通知进度条的进度   

注:如果要开启一个activity的话,必须给intent设置 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

事例

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:scaleType="centerCrop"
        android:src="@mipmap/ic_logo" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_toRightOf="@id/img"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:text="正在下载"
            android:id="@+id/txt"/>

        <ProgressBar
            android:id="@+id/pb"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true" />
    </LinearLayout>

</RelativeLayout>

在服务中发送一个通知


public class DownloadService extends Service {

    private Notification notification;
    private DownloadReceiver receiver;
    private final String ACTION_REPTY = "com.zhibo.jpm.action_repty";
    private MyApplication application;
    private String url;
    private RemoteViews views;
    private File file;

    public DownloadService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        url = intent.getStringExtra(CommonDefine.INTENT_URL);
        receiver = new DownloadReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_REPTY);
        registerReceiver(receiver, filter);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        views = new RemoteViews(getPackageName(), R.layout.layout_download);
        builder.setContent(views);
        views.setProgressBar(R.id.pb, 100, 50, true);
        builder.setSmallIcon(R.mipmap.ic_logo);
        notification = builder.build();
        startForeground(1000, notification);
        file = new File(Environment.getExternalStorageDirectory(), "zhibo.apk");
        application = (MyApplication) getApplication();

        download();

        return super.onStartCommand(intent, flags, startId);
    }

    private void download() {
        application.getUserAction().downLoadApk(url, file, new HttpClient.DownLoadListener() {

            private double time;

            @Override
            public void onError(String msg) {
                views.setTextViewText(R.id.txt, "下载失败请重试");
                Intent notificationIntent = new Intent(getApplicationContext(), DownloadReceiver.class);
                notificationIntent.setAction(ACTION_REPTY);
                PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
                notification.contentIntent = contentIntent;
                startForeground(1000, notification);
            }

            @Override
            public void inProgress(float progress, long total) {
                long l = System.currentTimeMillis();
                if (l - time > 2000) {
                    views.setTextViewText(R.id.txt, "正在下载(" + (int) (progress * 100) + "%)");
                    views.setProgressBar(R.id.pb, 100, (int) progress * 100, true);
                    startForeground(1000, notification);
                    time = l;
                }
            }

            @Override
            public void downloadOver() {
                views.setTextViewText(R.id.txt, "下载完成");
                startForeground(1000, notification);
                //安装应用
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                intent.addCategory("android.intent.category.DEFAULT");
                intent.setDataAndType(Uri.fromFile(file),
                        "application/vnd.android.package-archive");
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                stopSelf();
                application.finishAll();
            }
        });
    }

    private class DownloadReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case ACTION_REPTY:
                    download();
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }
}
### 回答1: Android通知是一种强大的功能,提供了多种通知和交互方,能够提醒用户重要的消息和事件。下面是Android通知的最全使用介绍。 首先,创建一个通知使用NotificationCompat.Builder类可以方便地构建通知。可以设置通知的标题、内容、图标等。 然后,设置通知的优先级和重要性。通过setPriority()方法设置通知的优先级,可以控制通知通知中的显示方。还可以使用setImportance()方法在Android 8.0以上的系统中设置通知的重要性级别。 接下来,设置通知的大文本样和大图样。通过setStyle()方法可以设置通知的样,如大文本样可以显示更多文字信息,大图样则可以显示一张大图作为通知的背景。 然后,设置通知进度条。如果需要在通知中显示进度,可以使用setProgress()方法设置通知的进度,并通过.setProgress()方法更新进度。 此外,还可以设置通知的声音、震动和闪光灯。使用setSound()方法设置通知的声音,setVibrate()方法设置通知的震动,setLights()方法设置通知的闪光灯。 还可以为通知添加操作按钮。使用addAction()方法添加操作按钮,可以在通知中显示按钮,并在用户点击按钮时执行相应的操作。 最后,发送通知使用NotificationManager类的notify()方法发送通知,可以指定通知的ID和要发送通知对象。 以上是Android通知最全的使用方法介绍。根据实际需求,可以选择其中的功能进行使用,以提升用户体验和提醒重要消息。 ### 回答2: Android的通知是一个非常重要的功能,它可以让用户快速查看和响应各种通知消息。下面是Android最全的通知使用的解释: 1. 创建通知对象:可以使用NotificationCompat.Builder类创建一个通知对象。你可以设置标题、内容、图标等属性。 2. 显示通知:通过NotificationManager类的notify()方法来显示通知。你需要指定一个唯一的通知ID来标识这个通知。 3. 自定义通知:Android提供了多种不同的通知,如大文本样、大图片样进度条等。你可以根据自己的需求选择合适的样,并使用setStyle()方法进行设置。 4. 添加操作按钮:你可以为通知添加操作按钮,让用户可以直接在通知进行一些操作或回复。通过addAction()方法可以添加一个或多个操作按钮。 5. 设置通知优先级:通过setPriority()方法可以设置通知的优先级,以决定通知通知中的显示顺序。优先级分为PRIORITY_MIN、PRIORITY_LOW、PRIORITY_DEFAULT、PRIORITY_HIGH和PRIORITY_MAX。 6. 设置通知点击跳转:当用户点击通知时,可以设置跳转到指定的Activity或执行特定的操作。通过setContentIntent()方法可以设置点击通知时的跳转目标。 7. 更新通知:如果你需要更新通知的内容或样,只需要再次调用NotificationManager的notify()方法,并指定之前的通知ID,新的通知将替换旧的通知。 8. 取消通知:通过NotificationManager的cancel()方法可以取消一个已显示的通知。如果你只想取消特定ID的通知,则使用cancel(id)方法。 总结来说,Android提供了丰富而全面的通知使用方法,使得我们可以根据实际需求创建、显示和管理通知。这些功能可以大大提升用户体验,让用户能够及时、方便地获取和响应各种通知信息。 ### 回答3: Android的通知是一个非常强大且灵活的功能,可以用于向用户提供及时的信息和交互。以下是Android最全的通知使用: 1. 创建通知使用NotificationCompat.Builder类创建通知对象,并设置标题、内容、图标等基本信息。 ``` NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("通知标题") .setContentText("通知内容"); Notification notification = builder.build(); ``` 2. 设置通知行为:可以为通知设置点击跳转到特定页面、打开特定链接等行为。 ``` Intent intent = new Intent(context, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); ``` 3. 设置大图样:可以为通知添加一个大图样,使其在展开时显示更多的内容。 ``` Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.large_image); NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle(); style.bigPicture(bitmap); builder.setStyle(style); ``` 4. 添加操作按钮:可以为通知添加一些操作按钮,比如回复、删除等。 ``` Intent deleteIntent = new Intent(context, DeleteReceiver.class); PendingIntent deletePendingIntent = PendingIntent.getBroadcast(context, 0, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT); builder.addAction(R.drawable.delete_icon, "删除", deletePendingIntent); ``` 5. 设置进度条:可以显示一个进度条来展示某个长时间任务的进度。 ``` builder.setProgress(100, progress, false); ``` 6. 添加扩展视图:可以为通知添加自定义的扩展视图,使其在展开时提供更多的交互和信息。 ``` RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.expanded_notification); builder.setCustomBigContentView(expandedView); ``` 以上仅是Android通知使用的一些常见功能,还有很多其他的使用和特性,开发者可以根据实际需求进行更多的定制和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值