Androidstudio制作订餐助手2小demo(复习通知栏、点击通知栏跳转、自定义通知栏提示音效)

 

Androidstudio制作订餐助手2小demo(复习通知栏、点击通知栏跳转、自定义通知栏提示音效)

 

                        ————安德风QQ1652102745

 

一、最终效果演示:

 

二、界面布局设计

1、首页界面布局设计activity_main.xml源代码

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:orientation="vertical"
 8     tools:context=".MainActivity" >
 9 
10     <ImageView
11         android:id="@+id/imageView6"
12         android:layout_width="126dp"
13         android:layout_height="150dp"
14         android:layout_marginStart="12dp"
15         android:layout_marginLeft="12dp"
16         android:layout_marginTop="148dp"
17         app:layout_constraintStart_toEndOf="@+id/imageView5"
18         app:layout_constraintTop_toTopOf="parent"
19         app:srcCompat="@drawable/hx" />
20 
21     <ImageView
22         android:id="@+id/imageView5"
23         android:layout_width="130dp"
24         android:layout_height="150dp"
25         android:layout_marginTop="148dp"
26         app:layout_constraintStart_toStartOf="parent"
27         app:layout_constraintTop_toTopOf="parent"
28         app:srcCompat="@drawable/cx" />
29 
30     <CheckBox
31         android:id="@+id/cb3"
32         android:layout_width="96dp"
33         android:layout_height="36dp"
34         android:layout_marginStart="44dp"
35         android:layout_marginLeft="44dp"
36         android:layout_marginTop="28dp"
37         android:text="叫花鸡(55元)"
38         app:layout_constraintStart_toEndOf="@+id/cb2"
39         app:layout_constraintTop_toBottomOf="@+id/imageView7" />
40 
41     <CheckBox
42         android:id="@+id/cb2"
43         android:layout_width="100dp"
44         android:layout_height="36dp"
45         android:layout_marginStart="44dp"
46         android:layout_marginLeft="44dp"
47         android:layout_marginTop="28dp"
48         android:text="海鲜美食(60元)"
49         app:layout_constraintStart_toEndOf="@+id/cb1"
50         app:layout_constraintTop_toBottomOf="@+id/imageView6" />
51 
52     <CheckBox
53         android:id="@+id/cb1"
54         android:layout_width="95dp"
55         android:layout_height="36dp"
56         android:layout_marginStart="16dp"
57         android:layout_marginLeft="16dp"
58         android:layout_marginTop="28dp"
59         android:text="川香排骨(45元)"
60         app:layout_constraintStart_toStartOf="parent"
61         app:layout_constraintTop_toBottomOf="@+id/imageView5" />
62 
63     <Button
64         android:id="@+id/pay"
65         android:layout_width="143dp"
66         android:layout_height="51dp"
67         android:layout_marginTop="68dp"
68         android:text="付款"
69         app:layout_constraintEnd_toEndOf="parent"
70         app:layout_constraintHorizontal_bias="0.518"
71         app:layout_constraintStart_toStartOf="parent"
72         app:layout_constraintTop_toBottomOf="@+id/cb2" />
73 
74     <ImageView
75         android:id="@+id/imageView7"
76         android:layout_width="130dp"
77         android:layout_height="150dp"
78         android:layout_marginStart="12dp"
79         android:layout_marginLeft="12dp"
80         android:layout_marginTop="148dp"
81         app:layout_constraintEnd_toEndOf="parent"
82         app:layout_constraintStart_toEndOf="@+id/imageView6"
83         app:layout_constraintTop_toTopOf="parent"
84         app:srcCompat="@drawable/jhj" />
85 
86     <TextView
87         android:id="@+id/textView5"
88         android:layout_width="wrap_content"
89         android:layout_height="wrap_content"
90         android:text="欢迎来到小浩订餐助手"
91         android:textSize="35sp"
92         app:layout_constraintBottom_toTopOf="@+id/imageView6"
93         app:layout_constraintEnd_toEndOf="parent"
94         app:layout_constraintStart_toStartOf="parent"
95         app:layout_constraintTop_toTopOf="parent" />
96 
97 </androidx.constraintlayout.widget.ConstraintLayout>

 

2、消费记录布局设计activity_main2.xml源代码

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical">
 7 
 8     <TextView
 9         android:id="@+id/tv"
10         android:layout_width="match_parent"
11         android:layout_height="wrap_content"
12         android:gravity="center_horizontal"
13         android:text="TextView"
14         android:textSize="24sp" />
15 </LinearLayout>

 

三、功能实现

1、下单功能实现MainActivity.java

  1 package com.example.my;
  2 
  3 import androidx.annotation.RequiresApi;
  4 import androidx.appcompat.app.AppCompatActivity;
  5 import androidx.core.app.NotificationCompat;
  6 
  7 import android.app.Notification;
  8 import android.app.NotificationChannel;
  9 import android.app.NotificationManager;
 10 import android.app.PendingIntent;
 11 import android.app.TaskStackBuilder;
 12 import android.content.Context;
 13 import android.content.ContextWrapper;
 14 import android.content.Intent;
 15 import android.content.pm.PackageManager;
 16 import android.graphics.Bitmap;
 17 import android.graphics.BitmapFactory;
 18 import android.graphics.Color;
 19 import android.media.Ringtone;
 20 import android.media.RingtoneManager;
 21 import android.net.Uri;
 22 import android.os.Build;
 23 import android.os.Bundle;
 24 import android.provider.Settings;
 25 import android.text.Layout;
 26 import android.view.LayoutInflater;
 27 import android.view.View;
 28 import android.widget.Button;
 29 import android.widget.CheckBox;
 30 import android.widget.TextView;
 31 import android.widget.Toast;
 32 
 33 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 34 CheckBox cb1,cb2,cb3;
 35 Button pay;
 36 int count=0;//声明为了统计金额
 37 String name="";//声明为了统计菜单名
 38 
 39 
 40     @RequiresApi(api = Build.VERSION_CODES.O)
 41     @Override
 42     protected void onCreate(Bundle savedInstanceState) {
 43         super.onCreate(savedInstanceState);
 44         setContentView(R.layout.activity_main);
 45 
 46         cb1=findViewById(R.id.cb1);
 47         cb2=findViewById(R.id.cb2);
 48         cb3=findViewById(R.id.cb3);
 49         pay=findViewById(R.id.pay);
 50         pay.setOnClickListener(this);
 51 
 52 
 53     }
 54 
 55     @RequiresApi(api = Build.VERSION_CODES.O)
 56     @Override
 57     public void onClick(View v) {
 58         if (cb1.isChecked())
 59         { count+=45;name+="川香排骨1份\n";}
 60         if (cb2.isChecked())
 61         {   count+=60;name+="海鲜美食1份\n";}
 62         if (cb3.isChecked())
 63         {   count+=55;name+="叫花鸡1份\n";}
 64 
 65 //
 66 //        final NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);//通知栏管理器(得到系统服务)
 67 //        String id = "channel_1"; //自定义设置通道ID属性
 68 //        String description = "123";//自定义设置通道描述属性
 69 //        int importance = NotificationManager.IMPORTANCE_HIGH;//通知栏管理重要提示消息声音设定
 70 //        /**
 71 //         * Oreo不用Priority了,用importance
 72 //         * IMPORTANCE_NONE 关闭通知
 73 //         * IMPORTANCE_MIN 开启通知,不会弹出,但没有提示音,状态栏中无显示
 74 //         * IMPORTANCE_LOW 开启通知,不会弹出,不发出提示音,状态栏中显示
 75 //         * IMPORTANCE_DEFAULT 开启通知,不会弹出,发出提示音,状态栏中显示
 76 //         * IMPORTANCE_HIGH 开启通知,会弹出,发出提示音,状态栏中显示*/
 77 //        NotificationChannel mChannel = new NotificationChannel(id, "123", importance);//建立通知栏通道类(需要有ID,重要属性)
 78 //        mChannel.setDescription(description); // 配置通知渠道的属性
 79 //        mChannel.enableLights(true);// 设置通知出现时的闪灯(如果 android 设备支持的话)
 80 //        mChannel.setLightColor(Color.RED);//设置闪灯颜色为红色
 81 //        mChannel.enableVibration(true);   // 设置通知出现时的震动(如果 android 设备支持的话)
 82 //        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
 83 //       manager.createNotificationChannel(mChannel);////最后在notificationmanager中创建该通知渠道
 84 //        Notification notification = new Notification.Builder(this, id)//创建Notification对象。
 85 //                .setContentTitle("付款通知")  //设置通知标题
 86 //                .setSmallIcon(R.drawable.q)//设置通知小图标
 87 //                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.q))//设置通知大图标
 88 //                .setContentText("您已付款"+count+"元")//设置通知内容
 89 //                .setDefaults(importance)
 90 //                .setAutoCancel(true)//设置自动删除通知
 91 //                .build();//运行
 92 //
 93 //        NotificationCompat.Builder builder=new NotificationCompat.Builder(getApplicationContext(),"default");
 94 //        //创建一个详情页Activity
 95 //        TaskStackBuilder taskStackBuilder=TaskStackBuilder.create(getApplicationContext());//①获取任务栈构造器
 96 //        taskStackBuilder.addParentStack(Main2Activity.class);//②把Activity实例添加到任务栈:调用任务栈构造器的addParentStack()方法添加Activity实例到任务栈中
 97 //        Intent intent=new Intent(MainActivity.this,Main2Activity.class);//③通过意图由当前页面跳转到指定的详情页面,并传输菜单名和金额数据
 98 //        intent.putExtra("name",name);
 99 //        intent.putExtra("count",count);
100 //        startActivity(intent);
101 //        taskStackBuilder.addNextIntent(intent);//④开始执行任务栈意图
102 //        //⑤调用任务构造器的getPendingIntent()方法获取一个PendingIntent对象。(该方法接收两个参数,第一个参数是整形请求码,第二个参数是PendingIntent的标识。)
103 //        PendingIntent pi=taskStackBuilder.getPendingIntent(1,PendingIntent.FLAG_UPDATE_CURRENT);
104 //        builder.setContentIntent(pi);//⑥调用通知构造器的setContentIntent()方法设置通知的内容意图
105 //        //⑦去清单文件程序里设置详情页的父Activity
106 //
107 //
108 //        manager.notify((int) System.currentTimeMillis(),notification); //通知栏保留多条通知
109 //        count=0;//清空金额
110 
111 //
112 //     LayoutInflater inflater=getLayoutInflater();
113 //     View layout=inflater.inflate(R.layout.activity_main2,null);
114 //     TextView tv= layout.findViewById(R.id.tv);
115 //     tv.setText("您支付了"+count+"元");
116 //     Toast toast=new Toast(MainActivity.this);
117 //     toast.setView(layout);
118 //     toast.setDuration(Toast.LENGTH_SHORT);
119 //     toast.show();
120 //     count=0;
121 
122 
123         int importance = NotificationManager.IMPORTANCE_HIGH;//通知栏管理重要提示消息声音设定
124         //第一步:创建通知构造器NotificationCompat.Builder对象。
125         NotificationCompat.Builder builder=new NotificationCompat.Builder(getApplicationContext(),"default");
126         //第二步:调用NotificationCompat.Builder对象的方法设置通知相关内容。
127         builder.setSmallIcon(R.drawable.q);//设置通知小图标
128         builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.q));//设置通知大图标
129         builder.setContentTitle("付款通知");//设置通知标题
130         builder.setContentText("您已付款"+count+"");//设置通知内容
131 
132 
133 
134 //        builder.setDefaults(Notification.DEFAULT_SOUND);//设置通知栏来通知的默认提示音
135         builder.setAutoCancel(true);//设置自动删除通知
136 
137         //创建一个详情页Activity
138         TaskStackBuilder taskStackBuilder=TaskStackBuilder.create(getApplicationContext());//①获取任务栈构造器
139         taskStackBuilder.addParentStack(Main2Activity.class);//②把Activity实例添加到任务栈:调用任务栈构造器的addParentStack()方法添加Activity实例到任务栈中
140         Intent intent=new Intent(MainActivity.this,Main2Activity.class);//③通过意图由当前页面跳转到指定的页面并将数据传输到详情页
141         intent.putExtra("name",name);
142         intent.putExtra("count",count);
143         startActivity(intent);
144         taskStackBuilder.addNextIntent(intent);//④开始执行任务栈意图
145         //⑤调用任务构造器的getPendingIntent()方法获取一个PendingIntent对象。(该方法接收两个参数,第一个参数是整形请求码,第二个参数是PendingIntent的标识。)
146         PendingIntent pi=taskStackBuilder.getPendingIntent(1,PendingIntent.FLAG_UPDATE_CURRENT);
147         builder.setContentIntent(pi);//⑥调用通知构造器的setContentIntent()方法设置通知的内容意图
148         //⑦去清单文件程序里设置详情页的父Activity
149 
150 
151         Notification notification=builder.build();//:创建Notification对象。
152 //        notification.defaults=Notification.DEFAULT_SOUND;//设置通知栏默认提示音
153         notification.sound=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.d);//设置通知栏自定义提示音
154         NotificationManager manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//通知栏管理器(得到系统服务)
155 //        manager.notify(1,notification); //通知栏保留单条通知
156         manager.notify((int) System.currentTimeMillis(),notification); //通知栏保留多条通知
157 //        count=0;//清空金额
158 
159 
160 
161     }
162 }

2、消费记录查询功能实现Main2Activity.java

 1 package com.example.my;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 
 5 import android.content.Intent;
 6 import android.os.Bundle;
 7 import android.widget.TextView;
 8 
 9 public class Main2Activity extends AppCompatActivity {
10 TextView tv;//声明文本内容输出
11     @Override
12     protected void onCreate(Bundle savedInstanceState) {
13         super.onCreate(savedInstanceState);
14         setContentView(R.layout.activity_main2);
15         tv=findViewById(R.id.tv);
16         Intent intent=getIntent();
17         String name=intent.getStringExtra("name");
18        int count=intent.getIntExtra("count",0);
19         tv.setText("您的消费记录:\n"+name+"\n您一共消费:"+count+"");
20 
21 
22 
23     }
24 
25 }

 

四、清单文件设置(AndroidManifest.xml)

在清单文件中添加在第二个activity详情页面添加(目的为了,用户返回时,能够通过任务栈形式返回到第一个页面中)

android:parentActivityName=".MainActivity"></activity>

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.example.my">
 4 
 5     <application
 6         android:allowBackup="true"
 7         android:icon="@mipmap/ic_launcher"
 8         android:label="@string/app_name"
 9         android:roundIcon="@mipmap/ic_launcher_round"
10         android:supportsRtl="true"
11         android:theme="@style/AppTheme">
12         <activity android:name=".Main2Activity" android:parentActivityName=".MainActivity"></activity>
13         <activity android:name=".MainActivity">
14             <intent-filter>
15                 <action android:name="android.intent.action.MAIN" />
16 
17                 <category android:name="android.intent.category.LAUNCHER" />
18             </intent-filter>
19         </activity>
20     </application>
21 
22 </manifest>

五、导入图片素材以及音频素材路径
1、导入图片素材路径:Res/drawable/XXX图片

2、导入音频素材路径:Res/raw/XXX音频

 

六、总结:

本次练习运用到了

①NotificationManager类:通知栏管理
②NotificationCompat类:通知类兼容(该类可以设置通知栏常用属性=》例如:设置通知栏的声音、标题、内容等)该类适合在Android8.0(API26)以下的版本适合用
③TaskStackBuilder类:通过任务栈类实现通告栏跳转到详情页面
④PendingIntent方法对象:该方法搭配任务构造器(TaskStackBuilder类)来实现通过栏参数接收。
⑤NotificationChannel通道类:该类是Android8.0(API26)及以上版本的新增特性,支持NotificationCompat类很多特性。当你的模拟器是Android8.0(Api26)
及以上版本时用户NotificationChannel通道类来写通知栏功能属性。不会运行异常
⑥自定义通知栏提示音效:
1  Notification notification=builder.build();//:创建Notification对象。
2 //        notification.defaults=Notification.DEFAULT_SOUND;//设置通知栏默认提示音
3         notification.sound=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.d);//设置通知栏自定义提示音

我是安德风,以上就是本次练习的基础知识概括,有问题欢迎留言及时纠正;感谢大家关注与支持,一起同进步~

 

posted @ 2020-04-06 18:39  安德风  阅读(804)  评论(0编辑  收藏  举报