zoukankan      html  css  js  c++  java
  • 安卓开发 手机通知栏发送提醒

    activity_main.xml文件按钮代码:

    <Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginLeft="26dp"
    android:layout_marginTop="14dp"
    android:text="点击发送" />
    <Button
    android:id="@+id/button2"
    android:layout_below="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_marginLeft="26dp"
    android:layout_marginTop="14dp"
    android:text="点击取消" />

    MainActivity.java文件代码:  

    public class MainActivity extends Activity implements OnClickListener{
    Button button1;
    Button button2;
    NotificationManager manager;
    int nid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);
    button1.setOnClickListener(this);
    button2.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()){
    case R.id.button1:
    Toast.makeText(this, "发送通知", Toast.LENGTH_SHORT).show();
    sendNotification();
    break;
    case R.id.button2:
    Toast.makeText(this, "取消通知", Toast.LENGTH_SHORT).show();
    manager.cancel(nid);
    break;
    }
    }

    private void sendNotification(){
    Intent intent = new Intent(this,MainActivity.class);
    PendingIntent pindtent = PendingIntent.getActivity(this, 0, intent, 0);
    Builder builder = new Notification.Builder(this);
    builder.setSmallIcon(R.drawable.img1);//图标
    builder.setTicker("hello");//手机状态栏的提示;
    builder.setWhen(System.currentTimeMillis());//设置时间
    builder.setContentTitle("通知的通知标题");
    builder.setContentText("sendnotification 发送的通知");//通知内容
    builder.setContentIntent(pindtent);//点击后意图
    //builder.setDefaults(Notification.DEFAULT_SOUND);//设置提示声音
    //builder.setDefaults(Notification.DEFAULT_LIGHTS);//设置指示灯
    //builder.setDefaults(Notification.DEFAULT_VIBRATE);//设置震动
    builder.setDefaults(Notification.DEFAULT_ALL);//设置提示声音,震动,指示灯
    Notification not = builder.build();
    manager.notify(nid,not);
    }

    }

  • 相关阅读:
    thinkphp3.1.3验证码优化
    php导出数据为CSV文件DEMO
    python学习笔记十七:base64及md5编码
    linux运维笔记
    [转]如何像Python高手(Pythonista)一样编程
    用gulp清除、移动、压缩、合并、替换代码
    [蓝桥杯][2017年第八届真题]小计算器(模拟)
    [蓝桥杯][2017年第八届真题]发现环(基环树输出环)
    [蓝桥杯][2017年第八届真题]合根植物(并查集)
    省赛训练5-3(四个签到题)
  • 原文地址:https://www.cnblogs.com/guojinyu/p/6667299.html
Copyright © 2011-2022 走看看