zoukankan      html  css  js  c++  java
  • Android开发笔记——通知栏设置

        手机有未接来电或未读短信时在通知栏就可以有一个状态,下拉后点击相应条目就可以进入此条目,同时通知栏上的此条信息消失,其实实现起来并不难,通过一个notification和notificationmanager即可

    我下面一个activity和一个intentservice实现,模拟下载程序:

    源码如下:

    package com.example.activity;

    import com.example.service.IntentServiceDemo;

    import android.os.Bundle;
    import android.app.Activity;
    import android.app.NotificationManager;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class Main extends Activity {

    @Override
    protected void onStart()
    {
    //点击bar跳转后bar消失,由于oncreate只有在创建时才条用故放在此方法中
    super.onStart();
    NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    manager.cancel(R.layout.main);


    }

    Button btn;
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    btn=(Button)findViewById(R.id.btn);
    btn.setOnClickListener(new OnClickListener()
    {

    @Override
    public void onClick(View v)
    {//点击按钮跳转至service,记得要在manifest文件中声明一下
    Intent intent=new Intent(Main.this,IntentServiceDemo.class);
    startService(intent);
    }
    });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }
    }

    点击按钮触发intentservice服务,

    注意到通知栏出现开始下载条目,5s过后下载完毕

    返回主菜单拉下通知栏

    点击条目可以来到main,同时通知栏中的条目消失

    ========================================

    package com.example.service;

    import com.example.activity.*;
    import android.app.IntentService;
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Intent;
    import android.util.Log;

    public class IntentServiceDemo extends IntentService
    {

    //需要一个空的构造方法,继承父类的名字

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

    @Override

    //重写onhandleintent方法
    protected void onHandleIntent(Intent intent)
    {
    Log.i("TAG","开始下载");

    //调用此函数 激发下载进程
    showNotification(true);
    try
    {

    //进程休眠5s,模拟下载过程
    Thread.sleep(5000);
    } catch (InterruptedException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    //下载完毕,再次调用
    showNotification(false);
    Log.i("TAG","下载完毕");

    }

    private void showNotification(boolean b)
    {
    Notification notification;//声明一个notification
    Intent intent=new Intent(this,Main.class);

    //pendingintent中封装了intent,跳转到Main
    PendingIntent contentIntent=PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationManager nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);//获取一个notificationmanager 
    if (b)//如果b为真表示还未下载成功开始下载
    {//三个参数分别为图标、标题、开始时间
    notification=new Notification(R.drawable.ok, "开始下载", System.currentTimeMillis());

    //下拉通知栏后显示的信息,点击后执行contentintent
    notification.setLatestEventInfo(this, "下载", "正在下载…", contentIntent);//用户点击时运行contentintent
    }else//否则为假 表示下载完毕
    {
    notification=new Notification(R.drawable.ok, "下载完毕", System.currentTimeMillis());
    notification.setLatestEventInfo(this, "下载", "下载完毕!", contentIntent);

    //可以指定通知的方式,如铃声、震动、LED彩灯等
    //notification.defaults=Notification.DEFAULT_LIGHTS;
    //notification.defaults=Notification.DEFAULT_SOUND;
    }

    //下载完毕后通知,参数为一个id(唯一),这里可以指定为R.layout.main,但前后要保持一致;另一个用来指定notification
    nm.notify(R.layout.main, notification);//id唯一的
    }

    }

  • 相关阅读:
    RedisTemplate使用事务处理
    maven命令学习
    springboot学习地址
    Mycat实现读写分离
    springboot-异步线程调用
    java多线程ExecutorService
    IntelliJ Idea 常用快捷键列表
    springMVC请求处理过程
    记录一次面试题
    java面试题-java内存模型
  • 原文地址:https://www.cnblogs.com/90zyh/p/2812332.html
Copyright © 2011-2022 走看看