为什么没有调用广播接收器的onreceive()方法?

时间:2015-10-12 13:14:46

标签: java android broadcastreceiver alarmmanager smsmanager

我是android开发的初学者。我正在尝试创建一个SMS调度程序app.My app的广播接收器的onReceive()方法没有被调用?下面给出的是我的源代码。我查了一些其他的链接,比如Basic Android alarm app, Broadcast Receiver's onReceive() method not being called请,我想知道我哪里出错了。谢谢。

  1. Homepage.java

        public class Homepage extends Activity
        {
         public EditText get_number;
        public EditText text_box;
        public String search_string,text_box_buffer;
        public Bundle bundle;
        public Intent mesg_intent;
        public PendingIntent pending_intent;
        public AlarmManager alarmManager;
        TimePickerFragment newFragment = new TimePickerFragment();
    
        public void showTimePicker(View view)
        {
            newFragment.show(getFragmentManager(),"timepicker");
        }
    
        public void messageSave(View view)
        {
            search_string=get_number.getText().toString();
            text_box_buffer=text_box.getText().toString();
            bundle.putCharSequence("number",search_string);
            bundle.putCharSequence("message", text_box_buffer);
            mesg_intent.putExtras(bundle);
            pending_intent = PendingIntent.getService(this,0,mesg_intent,0);
            alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC_WAKEUP,newFragment.getTime(),pending_intent);
            Toast.makeText(this,"SMS scheduled to be sent",Toast.LENGTH_SHORT).show();
        }
    
    
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_homepage);
            get_number = (EditText) findViewById(R.id.contacts_search);
            text_box = (EditText) findViewById(R.id.text_box);
            bundle = new Bundle();
            mesg_intent = new Intent(this,Alarmservice.class);
    
        }
    
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item)
        {
    
            int id = item.getItemId();
            if (id == R.id.action_settings)
            {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    

    }

  2. TimepickerFragment.java

    public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener
    

    {     public Calendar c = Calendar.getInstance();     public int hour = c.get(Calendar.HOUR_OF_DAY);     public int minute = c.get(Calendar.MINUTE);

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        return new TimePickerDialog(getActivity(),this,hour,minute,DateFormat.is24HourFormat(getActivity()));
    }
    
    public void onTimeSet(TimePicker view, int hourOfDay, int minuteset)
    {
        hour=hourOfDay;
        minute=minuteset;
    }
    public long getTime()
    {
        long time_ret = new GregorianCalendar().getTimeInMillis()+((hour*60*60*1000)+(minute*60*1000));
        return time_ret;
    }
    

    }

  3. Alarmservice.java

    public class Alarmservice extends BroadcastReceiver
    {
    @Override
    public void onReceive(Context context,Intent intent_local)
    {
        try {
            Bundle received_bundle = intent_local.getExtras();
            String number = (String) received_bundle.getCharSequence("number");
            String message = (String) received_bundle.getCharSequence("message");
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(number, null, message, null, null);
            Toast.makeText(context,"SMS successfully sent",Toast.LENGTH_SHORT).show();
        }
        catch (Exception e)
        {
            Toast.makeText(context,"SMS failed",Toast.LENGTH_SHORT).show();
        }
    }
    

    }

  4. 的Manifest.xml

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="24" />
    
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
    
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Homepage"
            android:label="@string/app_name"
            android:screenOrientation="sensor" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver
            android:process=":remote"
            android:name=".Alarmservice"/>
    </application>
    

1 个答案:

答案 0 :(得分:1)

问题在于创建意图,请执行以下操作:

mesg_intent = new Intent(this, Alarmservice.class);
mest_intent.putExtras(bundle);
pending_intent = PendingIntent.getBroadcast(this, 0, mesg_intent, PendingIntent.FLAG_UPDATE_CURRENT);