You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android来电/去电监听无Toast提示问题求助

Hey there! Let's figure out why your Toast messages aren't popping up for incoming or outgoing calls. I've gone through your code and spotted a couple of key issues that are likely causing this problem.

1. Missing Runtime Permissions (Critical for Android 6.0+)

Starting from Android 6.0 (API level 23), dangerous permissions like READ_PHONE_STATE and PROCESS_OUTGOING_CALLS need to be requested dynamically at runtime—not just declared in the manifest. Your current code only adds them to AndroidManifest.xml, which isn't enough for newer Android versions.

Here's how to add runtime permission checks in your MainActivity:

package com.example.arek.test;

import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

public class MainActivity extends AppCompatActivity {
    private static final int PERMISSION_REQUEST_CODE = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Check and request required permissions
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, Manifest.permission.PROCESS_OUTGOING_CALLS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_PHONE_STATE, Manifest.permission.PROCESS_OUTGOING_CALLS},
                    PERMISSION_REQUEST_CODE);
        } else {
            Toast.makeText(getApplicationContext(), "Permissions already granted!", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PERMISSION_REQUEST_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
                    && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getApplicationContext(), "Permissions granted successfully!", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(), "Permissions are required for this app to work properly!", Toast.LENGTH_LONG).show();
            }
        }
    }
}

2. Ensure Broadcast Receiver Triggers Correctly

Your manifest registration for CallReceiver looks correct, but double-check these details:

  • The class name .CallReceiver matches your actual class (it does in your code, so this is probably fine)
  • Your device/emulator isn't restricting background broadcasts (some OEMs limit this for battery saving—try testing on a stock Android device if possible)

3. Optimize Toast Display in Broadcast Receiver

Broadcast receivers run on a background thread, so using the main thread to show Toast can help ensure it appears reliably. Modify your CallReceiver methods like this:

import android.os.Handler;
import android.os.Looper;

// ...

@Override
protected void onIncomingCallStarted(Context ctx, String number, Date start) {
    new Handler(Looper.getMainLooper()).post(() -> {
        Toast.makeText(ctx.getApplicationContext(), "Incoming call from: " + number, Toast.LENGTH_LONG).show();
    });
}

@Override
protected void onOutgoingCallStarted(Context ctx, String number, Date start) {
    new Handler(Looper.getMainLooper()).post(() -> {
        Toast.makeText(ctx.getApplicationContext(), "Outgoing call to: " + number, Toast.LENGTH_LONG).show();
    });
}

4. Test on a Physical Device

Some emulators have issues simulating phone state changes accurately. If possible, test your app on a real Android device to rule out emulator-specific bugs.

Final Steps

  • Uninstall the old version of your app from the device/emulator before reinstalling the updated code
  • Make sure you grant the requested permissions when the app prompts you

Once you implement these fixes, your Toast messages should start appearing when calls come in or go out!

内容的提问来源于stack exchange,提问作者Arkadiusz G.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:50:20