[Day29] 悬浮视窗提醒

这次要来教的是如何制作一个悬浮视窗的提醒,这个功能在许多目前市面上常用的APP上都能看到,例如:Line、FB等....都有这种提醒的功能,现在就来示范如何使用此功能。

XML

先简单设计出一个页面,写出一个Button作为触发悬浮视窗的元件就好。

<?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="match_parent" >
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮"/>
</RelativeLayout>

JAVA程序

设计好XML之後,就能开始制作悬浮视窗的功能了。
先做好按钮的监听器,接着在触发功能输入触发悬浮视窗的事件,下面先解析一些程序码的片段。
Manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 此为手机通知的总管理

isOpened = Manager.areNotificationsEnabled();
if(!isOpened){
    Toast.makeText(MainActivity.this,"请开启通知权限", Toast.LENGTH_SHORT).show();
    Intent intent = new Intent();
    intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
    intent.putExtra(Settings.EXTRA_APP_PACKAGE, MainActivity.this.getPackageName());
    intent.putExtra(Settings.EXTRA_CHANNEL_ID, MainActivity.this.getApplicationInfo().uid);
    MainActivity.this.startActivity(intent);
}

这段用来判断是否有开启通知,如没有则导入到系统中
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);设定NotificationChannel的ID和NAME,第三个参数是用以表示通知的重要性

下面附上完整程序码

public class MainActivity extends AppCompatActivity {
    Button button;
    boolean isOpened;
    String channelId = "channel1";
    String channelName = "name";
    NotificationManager Manager;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isOpened = Manager.areNotificationsEnabled();
                if(!isOpened){
                    Toast.makeText(MainActivity.this,"请开启通知权限", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent();
                    intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
                    intent.putExtra(Settings.EXTRA_APP_PACKAGE, MainActivity.this.getPackageName());
                    intent.putExtra(Settings.EXTRA_CHANNEL_ID, MainActivity.this.getApplicationInfo().uid);
                    MainActivity.this.startActivity(intent);
                }

                NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
                Manager.createNotificationChannel(channel);
                Notification.Builder builder = new Notification.Builder(MainActivity.this,channelId);
                builder.setSmallIcon(R.drawable.aa)
                        .setChannelId(channelId)
                        .setContentTitle("标题")
                        .setContentText("内容").build();

                Manager.createNotificationChannel(channel);
                Manager.notify(1, builder.build());
            }
        });
    }
}

AndroidManifest

最後在AndroidManifest中加入权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
加入完後才能进到设定页面。

成果

图片


<<:  OK集#28-白话文Excel-文字合并

>>:  Day30- 最後完成代办事项 To DO List 小工具

Day2关於『程序』的起源和特性&演算法

最一开始的程序是机器代码(machine code),演变成组合代码(assembly code),...

【第二十三天 - DFS 题目分析】

先简单回顾一下,今天预计分析的题目: 112. Path Sum 题目连结:https://lee...

Day02 - React component 初认识

React component React componemt目的在於将制作网页时,将重复用到的功能...

【设计+切版30天实作】|Day14 - 简约CTA的用处及设计的注意事项

设计大纲 设计CTA的用意一方面是让结尾不会来得太突然,另一方面是想在网页的最後再来一个「Call ...

Day13:终於要进去新手村了-Javascript-判断式基本结构-if…else

今天要来提到的是判断式的部分,在JS中判断式有两种语法:if...else和 switch。 这篇我...