Monitor项目开发走过的路~透明Dialog型Activity

来源:互联网 发布:ipad淘宝hd微淘在哪 编辑:程序博客网 时间:2024/05/17 22:43

  在Monitor项目开发的过程中之前遇到一个问题:我在前台服务中开启了与服务器的Socket长连接,要长时间保持网络连接操作。但是想到可能用户会想要某一时刻关闭连接,或者说退出整个APP应用。为了实现这个效果首先得要点击Notification时有一个提示框,来与用户进行交互确认是否要进行关闭操作。首先想到的是能不能直接弹出一个Dialog出来,但是发现Notification的PendingIntent只能传入一个Intent来响应点击操作,于是就想到了之前看书时看到的Dialog型Activity,它外形和普通Dialog一样,不过随时通过Intent来启动这个透明效果的Activity.首先我们像创建普通Activity一样创建一个DialogActivty

public class DialogActivity extends AppCompatActivity implements View.OnClickListener{    private Button yesButton;    private Button noButton;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_dialog);        MangerApplication.getInstance().addActivity(this);        yesButton=(Button)findViewById(R.id.dialog_yes);        noButton=(Button)findViewById(R.id.dialog_no);        yesButton.setOnClickListener(this);        noButton.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()){            case R.id.dialog_yes:                //正常退出App                MangerApplication.getInstance().exit();                break;            case R.id.dialog_no:                finish();                break;            default:                break;        }    }}

  下面是布局activity_dialog.xml文件

<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.CardView    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="120dp"    app:cardCornerRadius="8dp"    app:cardBackgroundColor="#c9f9f2"    tools:context="com.qinlong275.android.monitor.DialogActivity">    <android.support.constraint.ConstraintLayout        android:layout_width="match_parent"        android:layout_height="match_parent">        <TextView            android:layout_width="368dp"            android:layout_height="wrap_content"            android:text="您确定要退出ZeroMeter连接么"            android:textColor="#000000"            android:textSize="20sp"            android:gravity="center"            android:layout_marginLeft="8dp"            app:layout_constraintLeft_toLeftOf="parent"            android:layout_marginRight="8dp"            app:layout_constraintRight_toRightOf="parent"            app:layout_constraintTop_toTopOf="parent"            android:layout_marginTop="8dp"/>        <Button            android:id="@+id/dialog_yes"            android:layout_width="100dp"            android:layout_height="40dp"            app:layout_constraintBottom_toBottomOf="parent"            android:layout_marginBottom="8dp"            android:layout_marginLeft="50dp"            android:text="Yes"            app:layout_constraintLeft_toLeftOf="parent"/>        <Button            android:id="@+id/dialog_no"            android:layout_width="100dp"            android:layout_height="40dp"            app:layout_constraintBottom_toBottomOf="parent"            android:layout_marginBottom="8dp"            android:layout_marginRight="50dp"            android:text="No"            app:layout_constraintRight_toRightOf="parent"/>    </android.support.constraint.ConstraintLayout></android.support.v7.widget.CardView>

  还有最重要的一步要在AndroidManifest.xml中声明此Activity的Theme属性,这样才能创建出一个Dialog型的Activity负责就会像普通的Activity一样占满整个屏幕。

<activity android:name=".DialogActivity"       android:theme="@style/dialog"></activity>

  接下来就是利用Intent启动这个DialogActivity即可,下面是创建通知栏Notification代码,我们将Intent传入其中。

private Notification getNotification(String title) {        Intent intent = new Intent(this, DialogActivity.class);        PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);        builder.setSmallIcon(R.mipmap.logo);        builder.setContentIntent(pi);        builder.setContentTitle(title);        return builder.build();    }

 下面是最终的效果图,无论你在哪个应用界面点击通知栏Notification都会触发其中的PendingIntent来启动创建好的DialogActivity。你可以在这个新的Activity中与用户进行交互响应。

这里写图片描述

这里写图片描述

阅读全文
0 0