Android - 如何添加Alert Dialog进入呼叫?

时间:2018-01-01 22:23:03

标签: android apk

我测试来自https://gist.github.com/ftvs/e61ccb039f511eb288ee的代码,我希望在接到来电时进行警告对话

这是我的代码(但吐司工作): CallReveiver.java:

package com.example.arek.test;

import android.content.Context;
import android.widget.Toast;

import java.util.Date;

public class CallReceiver extends PhonecallReceiver {
    public static String data = "";
    fetchData process;
    @Override
    protected void onIncomingCallStarted(Context ctx, String number, Date start) {

        process = new fetchData(ctx.getApplicationContext());
        process.execute();
    }

fetchData.java

package com.example.arek.test;

import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class fetchData extends AsyncTask<Void,Void,Void> {
    String data = "";
    Context context;
    public fetchData(Context mContext) {
        this.context = mContext;
    }
    @Override
    protected Void doInBackground(Void... voids) {
        try {
            [...]
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        Toast.makeText(context, this.data,Toast.LENGTH_LONG).show(); //WORKING

        //Alert no wrking...
        AlertDialog alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Alert");
        alertDialog.setMessage("Example message");
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        alertDialog.show();
    }

}

如果我模拟进入呼叫,则应用程序崩溃...上下文问题... 请帮我?谢谢!

2 个答案:

答案 0 :(得分:1)

这是不可能的。弹出这些屏幕的应用程序是一个单独的应用程序。没有用于向其添加文本的API。您可以编写自己的拨号器应用程序,让用户安装它,并提供用户期望的所有功能。但是没有办法将它注入他们的应用程序。

答案 1 :(得分:0)

吐司作品

Toast toast = Toast.makeText(getApplicationContext(), 
"Text", Toast.LENGTH_SHORT); 
toast.show();