1

我已注册 ContentObserver 以收听传出消息。即使这样,我也只能收听传入的消息,但不能收听传出的消息。当我发送任何短信时,没有调用 onChange()

这是我的代码:

传出SMSActivity.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>

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

4

2 回答 2

0

将“注册到 Content Observer ”部分移至服务解决了该问题。

于 2013-09-25T06:16:21.500 回答
0

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

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

希望这可以帮助

于 2013-09-24T12:56:20.540 回答