Android学习:Notification状态栏通知

 

Notification是显示在手机状态栏的通知,它代表一种具有全局效果的通知,程序一般通过NotificationManager服务来发送Notification。
在小米手机上,手指在屏幕顶端向下划,可以看到这个通知内容。

package allegro.notificationtest;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { static final int NOTIFICATION_ID = 0x123; NotificationManager nm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); nm=(NotificationManager) getSystemService(NOTIFICATION_SERVICE); } public void send(View source) { Intent intent=new Intent(MainActivity.this,OtherActivity.class); PendingIntent pi= PendingIntent.getActivity(MainActivity.this,0,intent,0); Notification notify = new Notification.Builder(this) .setAutoCancel(true) // 设置显示在状态栏的通知提示信息 .setTicker("有新消息") // 设置通知的图标  .setSmallIcon((R.drawable.java)) // 设置通知内容的标题 .setContentTitle("一条新通知") // 设置通知内容 .setContentText("恭喜你,你中奖了!") // 设置使用系统默认的声音、默认LED灯 // .setDefaults(Notification.DEFAULT_SOUND // |Notification.DEFAULT_LIGHTS) // 设置通知的自定义声音 // .setSound(Uri.parse("android.resource://org.crazyit.ui/" // + R.raw.msg))  .setWhen(System.currentTimeMillis()) // 设改通知将要启动程序的Intent .setContentIntent(pi) //设置事件信息,这里启动intent对应的程序。
 .build(); nm.notify(NOTIFICATION_ID,notify); } public void del(View v) { nm.cancel(NOTIFICATION_ID); } }

 

 

 

点击发送按钮后,划开通知栏点击通知,执行启动OtherActivity程序

 另外,AndroidManifest.xml文件中声明权限。因为程序要访问闪光灯,振动器。

        <!-- 添加操作闪光灯的权限 -->
        <uses-permission android:name="android.permission.FLASHLIGHT"/>
        <!-- 添加操作振动器的权限 -->
        <uses-permission android:name="android.permission.VIBRATE"/>

 

转载于:https://www.cnblogs.com/hackpig/p/8578931.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值