SMS不与IntentService一起发送

时间:2017-07-06 12:09:52

标签: android android-intentservice

我尝试使用IntentService向某些电话号码发送消息,但我不知道为什么我没有收到消息。这是我的代码:

public class NotificationService extends IntentService {

public NotificationService() {
    super(NotificationService.class.getSimpleName());
}

@Override
protected void onHandleIntent(Intent intent) {
    String number, message;

    number = (String) intent.getExtras().get("NUMBER");
    message = (String) intent.getExtras().get("MESSAGE");

    sendMessage(number, message);
}

private void sendMessage(String number, String message) {
    final Intent intent;
    intent = new Intent(getApplicationContext(), SentActivity.class);
    intent.putExtra("NUMBER", number);
    intent.putExtra("MESSAGE", message);
    startActivity(intent);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
            0, intent, 0);
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(number, null, message, pendingIntent, null);
}
}

这就是我称之为服务的地方

private View.OnClickListener mButtonClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        int id = v.getId();

        switch (id) {
            case R.id.send_button:
                number = mPhoneNumber.getText().toString();
                message = mComposeMessage.getText().toString();
                if (number.length() > 0 && message.length() > 0){
                    Intent serviceIntent = new Intent(v.getContext(), NotificationService.class);
                    serviceIntent.putExtra("MESSAGE", message);
                    serviceIntent.putExtra("NUMBER", number);
                    startService(serviceIntent);
                }
                break;

            case R.id.cancel_button:
                break;
        }
    }
};

这是在我的AndroidManifest.xml

<uses-permission android:name="android.permission.SEND_SMS"/>
<service android:name=".service.NotificationService"
        android:exported="true"/>

0 个答案:

没有答案