BroadcastReceiver中的Toasts保持可见时间过长

时间:2012-09-07 08:55:23

标签: android

情况:

我有Service(也尝试使用Activity),每次点击按钮都会发送短信。我99.9%肯定方法sendSMS(...)只被调用一次。 Toasts在屏幕上BroadcastReceiver内创建的"SMS Sent",可以在几秒钟之间,几乎是在相反的情况下,直到应用程序被强制停止。 Toast在屏幕上停留了一段时间,SMS发送成功。您可以看到BroadcastReceiver淡入淡出。

我做错了吗?

加成:

为什么具有"SMS delivered"状态的 @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); sendSMS("5555555","hello world"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); sendSMS("5555555","hello world"); return Service.START_STICKY; } // ---sends an SMS message to another device--- private void sendSMS(final String phoneNumber, final String message) { String SENT = "SMS_SENT"; String DELIVERED = "SMS_DELIVERED"; PendingIntent sentPI = PendingIntent.getBroadcast(this.getApplicationContext(), 0, new Intent( SENT), 0); PendingIntent deliveredPI = PendingIntent.getBroadcast(this.getApplicationContext(), 0, new Intent(DELIVERED), 0); // ---when the SMS has been sent--- registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(context, "SMS sent", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: Toast.makeText(context, "SMS error: Generic failure", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NO_SERVICE: Toast.makeText(context, "SMS error: No service", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NULL_PDU: Toast.makeText(context, "SMS error: Null PDU", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_RADIO_OFF: Toast.makeText(context, "SMS error: Radio off", Toast.LENGTH_SHORT).show(); break; } } }, new IntentFilter(SENT)); // ---when the SMS has been delivered--- registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(context, "SMS delivered", Toast.LENGTH_SHORT).show(); break; case Activity.RESULT_CANCELED: Toast.makeText(context, "SMS not delivered", Toast.LENGTH_SHORT).show(); break; } } }, new IntentFilter(DELIVERED)); SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); } 不能获得任何回复?

这是我很久以前从教程中抓取的非常通用的代码:

{{1}}

/编辑:修正问题,因为您可以在此问题的评论中阅读

感谢您的光临

3 个答案:

答案 0 :(得分:0)

您可以使用this方法以低于Toast.LENGTH_SHORT的价格展示您的祝酒词。

答案 1 :(得分:0)

两次致电sendSMS(),我的服务已弃用onStart()罪魁祸首

@Override
public void onStart(Intent intent, int startId) {
    //super.onStart(intent, startId);
    sendSMS("5555555","hello world");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    //super.onStartCommand(intent, flags, startId);
    sendSMS("5555555","hello world");
    return Service.START_STICKY;
}

This answer helped me keep both methods

答案 2 :(得分:-3)

用它来显示

Toast.makeText(context,message,1000).show();

其中1000是以毫秒为单位的持续时间