【问题标题】:Why is Broadcast receiver's onreceive() method not invoked?为什么广播接收器的 onreceive() 方法没有被调用?
【发布时间】:2016-01-09 23:45:59
【问题描述】:

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

  1. 主页.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
    

    { 公共日历 c = Calendar.getInstance(); 公共 int 小时 = 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. 报警服务.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>
    

【问题讨论】:

  • 你的表现如何?
  • 尝试PendingIntent.getBroadcast 而不是PendingIntent.getService
  • @StefanBeike 添加了清单文件

标签: java android broadcastreceiver alarmmanager smsmanager


【解决方案1】:

问题在于创建意图,这样做:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    相关资源
    最近更新 更多