onChange()没有在传出短信上被调用

时间:2013-09-24 12:46:47

标签: android sms contentobserver

我已注册ContentObserver来收听外发邮件。即便如此,我只能听到传入的消息而不是传出的消息。我发送任何短信时都没有调用onChange()

这是我的代码:

OutgoingSMSActivity.java:

package com.test.outgoingsms;

public class OutgoingSMSActivity extends Activity implements OnClickListener{

private Button mSendSMS;
private static final String TAG = "OutgoingSMSActivity";
private SMSObserver lObserver;

public OutgoingSMSActivity() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_outgoing_sms);

    mSendSMS = (Button) findViewById(R.id.sendsms);
    mSendSMS.setOnClickListener(this);
    Handler handler = new Handler();
    lObserver = new SMSObserver(handler);
    getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, lObserver);
    getContentResolver().registerContentObserver(Uri.parse("content://mms-sms/conversations"), true, lObserver);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    getContentResolver().unregisterContentObserver(lObserver);
}


private void sendMessage() {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);

    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);

    // ---when the SMS has been sent---
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                Toast.makeText(getBaseContext(), "No service", Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_SHORT).show();
                break;
            }
        }
    }, new IntentFilter(SENT));

    // ---when the SMS has been delivered---
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(getBaseContext(), "SMS delivered", Toast.LENGTH_SHORT).show();
                break;
            case Activity.RESULT_CANCELED:
                Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
                break;
            }
        }
    }, new IntentFilter(DELIVERED));

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage("+91XXXXXXXXX", null, "Hello..", sentPI, deliveredPI);
}

public class SMSObserver extends ContentObserver {

    public SMSObserver(Handler handler) {
        super(handler);
    }

    @Override
    public boolean deliverSelfNotifications() {
        return true;
    }

    @Override
    public void onChange(boolean selfChange) {
        Log.e(TAG,"onChange");
        super.onChange(selfChange);
        Toast.makeText(getApplicationContext(), "SMS is being sent!!!", Toast.LENGTH_LONG).show();

    }

}

@Override
public void onClick(View v) {
    if(v.getId() == R.id.sendsms){
        sendMessage();
        }
    }

}

这是我的清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.outgoingsms"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="15" />

<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".OutgoingSMSActivity"
        android:label="@string/title_activity_outgoing_sms" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

如果我在这里遗漏任何东西,请告诉我。

2 个答案:

答案 0 :(得分:0)

您必须在清单文件中添加Intent Filter

   <receiver android:name="com.test.outgoingsms"> 
                <intent-filter>                                         
                    <action   android:name="android.provider.Telephony.SMS_SENT"/>
                </intent-filter> 
     </receiver>

希望这有帮助

答案 1 :(得分:0)

将“注册到内容观察者”部分移至该服务解决了该问题。