简体   繁体   English

如何使用AlarmManager和广播接收器发送文本?

[英]How to send text using AlarmManager and broadcast receiver?

I am writing an app that takes in EditText data from the user and sets an alarm with the data collected using AlarmManager and then will send a text Using broadcast receiver. 我正在编写一个应用程序,它接收来自用户的EditText数据,并使用AlarmManager收集数据设置警报,然后发送文本使用广播接收器。 My small toast stating the alarm has been set works so i know the data is being gathered correctly but i never receive a text. 我的小吐司说明了警报已经设置工作,所以我知道数据正在正确收集,但我从来没有收到文本。 What is happening? 怎么了?

Here is my main activity where I collect the data and set the alarm: 这是我收集数据并设置警报的主要活动:

import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void scheduleAlarm(View V)
    {
        EditText timeField = (EditText) findViewById(R.id.editText1);
            int time = Integer.parseInt(timeField.getText().toString());
        EditText dayField = (EditText) findViewById(R.id.editText2);
            int day = Integer.parseInt(dayField.getText().toString());
        EditText monthField = (EditText) findViewById(R.id.editText3);
            int month = Integer.parseInt(monthField.getText().toString());


        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        calendar.set(Calendar.HOUR_OF_DAY, time);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        Intent intentAlarm = new Intent(this, AlarmReciever.class);
        PendingIntent pIntent =  PendingIntent.getBroadcast(this.getApplicationContext(), 234324243,  intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
        Toast.makeText(this, ("Alarm scheduled for " + month + "/" + day + " at " + time + "pm"), Toast.LENGTH_LONG).show();

    }    

}

Here is my broadcast reciever: 这是我的广播接收器:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;

public class AlarmReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent)
    {       
            String phoneNumberReciver="0000000000"; //my phone number usually entered here
            String message="Hi I will be there later, See You soon";
            SmsManager sms = SmsManager.getDefault(); 
            sms.sendTextMessage(phoneNumberReciver, null, message, null, null);

     }
}

I have also made sure to edit my manifest with these permissions: 我还确保使用这些权限编辑我的清单:

<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<receiver android:name=".AlarmReciever"/>

but here is the whole xml: 但这里是整个xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bpa"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.bpa.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

         <receiver android:name=".AlarmReciever"/>

    </application>

</manifest>

Try declaring the receiver in your manifest with a fully qualified package name: 尝试使用完全限定的包名称声明清单中的接收者:

<receiver android:name="com.myapp.AlarmReciever"/>

If that still doesn't work you may need to specify an action that your receiver handles, and then when you create the alarm intent, set the action to the intent. 如果仍然不起作用,您可能需要指定接收器处理的操作,然后在创建警报意图时,将操作设置为意图。 eg: 例如:

       <receiver android:name="AlarmReceiver" >
            <intent-filter>
                <action android:name="com.myapp.mybroadcast" />
            </intent-filter>
        </receiver>

Intent intentAlarm = new Intent(this, AlarmReciever.class);
intent.setAction("com.myapp.mybroadcast");

you could add a broadcast receiver to receive the SMS_SENT intent. 您可以添加广播接收器以接收SMS_SENT意图。 this would help you determine if the issue is that the SMS is never sent vs. your Handset never receives it for some reason (eg, SMSC/routing issues, etc.) 这将有助于您确定问题是否从未发送过SMS而您的手机由于某种原因从未收到过(例如,SMSC /路由问题等)

here is an example of such a broadcast receiver: 这是一个这样的广播接收器的例子:

        //---when the SMS has been sent---
        smsReceiver = new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                String result = "";

                switch (getResultCode())
                {
                case Activity.RESULT_OK:
                    result = "Message sent";     
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    result = "Generic failure";                           
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    result = "No service";
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    result = "Null PDU";
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    result = "Radio off";
                    break;
                }
                Toast.makeText(context, result + ": " + message, Toast.LENGTH_LONG).show();
                Log.d(TAG, "<onReceive> received SMS sent intent: " + result);

            }
        };

then you register the intent and use this to send the SMS: 然后你注册意图并用它来发送短信:

registerReceiver(smsReceiver, new IntentFilter("SMS_SENT"));
// SMS sent pending intent
PendingIntent sentIntent = PendingIntent.getBroadcast(this, 0, new Intent("SMS_SENT"), 0);
// send the SMS
sms.sendTextMessage(phoneNumberReciver, null, message, sentIntent, null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM