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

Android接收SMS时Toast不显示问题求助(附配置与代码)

Fixing SMS Receiver Toast Not Showing Up

Hey Reza, let's troubleshoot why your SMS receiver's Toast isn't popping up. I’ve reviewed your code and manifest, and here are the key fixes you need to implement:

1. Add Runtime Permissions for Android 6.0+

Starting from Android 6.0 (API level 23), dangerous permissions like RECEIVE_SMS require explicit user approval at runtime—just declaring them in the manifest isn’t enough.

Add this code to your MainActivity to request the necessary permissions when the app launches:

public class MainActivity extends AppCompatActivity {
    private static final int SMS_PERMISSION_REQUEST_CODE = 101;

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

        // Check and request SMS permissions
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.RECEIVE_SMS},
                    SMS_PERMISSION_REQUEST_CODE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == SMS_PERMISSION_REQUEST_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission granted, receiver should work now
                Toast.makeText(this, "SMS permissions granted!", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "SMS permissions denied—receiver won't work", Toast.LENGTH_LONG).show();
            }
        }
    }
}

2. Fix the Toast Context in Your Receiver

Using your global G.context might cause issues in broadcast receivers (application context can sometimes behave unexpectedly with UI elements like Toast). Instead, use the context parameter passed directly to onReceive():

public class smsReciver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Use the provided context instead of G.context
        Toast.makeText(context, "SMS received!", Toast.LENGTH_LONG).show();
    }
}

3. Add Priority to Your Receiver's Intent Filter

Other apps or system services might be intercepting the SMS broadcast before your receiver gets it. Adding a higher priority to your intent filter ensures your receiver is called earlier:

<receiver android:name=".smsReciver">
    <intent-filter android:priority="1000"> <!-- Higher priority increases chances of being called first -->
        <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
    </intent-filter>
</receiver>

4. Quick Compatibility & Sanity Checks

  • For Android 8.0+ (API 26): SMS_RECEIVED is still allowed for statically registered receivers, but double-check your target SDK version and ensure runtime permissions are properly handled.
  • Test on a real device instead of an emulator—some emulators have inconsistent SMS broadcast handling.
  • Confirm no other app is set as the default messaging app (default apps get priority for SMS broadcasts).
  • Double-check your G class is correctly declared in the manifest (your current setup looks good, but it’s worth a quick verification).

Once you implement these fixes, your SMS receiver should trigger the Toast correctly when a message arrives.

内容的提问来源于stack exchange,提问作者reza motahari

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:57:30