广播接收器无法处理Fragment的警报

时间:2014-07-19 11:47:38

标签: android android-fragments android-edittext broadcastreceiver android-alarms

我正在处理带有Fragments的应用程序,其中一个片段包含警报功能。 为此,我使用了广播接收器,但不明白为什么它不起作用: 以下是代码,我正在使用PC"而不是移动设备"

public class SchedulerListFragment extends Fragment {
    PendingIntent pendingIntent;
    AlarmManager alarmManager;
    BroadcastReceiver mReceiver;
    EditText ethr,etmin,etsec;
    int result = 1;
    int hr = 0;
    int min = 0;
    int sec = 0;
    Button alarm;
    IntentFilter filter1;

    private void RegisterAlarmBroadcast()
    {       
        mReceiver = new BroadcastReceiver()
        {
           // private static final String TAG = "Alarm Example Receiver";
            @Override
            public void onReceive(Context context, Intent intent)
            {
                Toast.makeText(context, "Alarm time has been reached", Toast.LENGTH_LONG).show();
            }
        };

        getActivity().registerReceiver(mReceiver, new IntentFilter("com.example.lightcontrolsystem.SchedulerFragment.class") );
        pendingIntent = PendingIntent.getBroadcast( getActivity(), 0, new Intent("sample"),0 );
        alarmManager = (AlarmManager)(getActivity().getSystemService( Context.ALARM_SERVICE ));
    }


    /*
    private void UnregisterAlarmBroadcast()
    {
        alarmManager.cancel(pendingIntent); 
        getBaseContext().unregisterReceiver(mReceiver);
    }
*/

    Button.OnClickListener buttonalarmOnClickListener = new Button.OnClickListener() {

        public void onClick(View v) {
             String shr = ethr.getText().toString();
                if(shr.equals(""))
                {
                    hr = 0;
                }
                else
                {
                    hr = Integer.parseInt(ethr.getText().toString());
                    hr=hr*60*60*1000;
                }

                String smin = etmin.getText().toString();
                if(smin.equals(""))
                {
                    min = 0;
                }
                else
                {
                     min = Integer.parseInt(etmin.getText().toString());
                     min = min*60*1000;

                }

                String ssec = etsec.getText().toString();
                if(ssec.equals(""))
                {
                    sec = 0;
                }
                else
                {
                     sec = Integer.parseInt(etsec.getText().toString());
                     sec = sec * 1000;

                }

                result = 50*1000; //hr+min+sec;
                Toast.makeText(getActivity(), "Alarm SET", Toast.LENGTH_LONG).show();
                alarmManager.set( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + result , pendingIntent);
        //      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), result , pendingIntent); 


        }
    }; 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view=inflater.inflate(R.layout.scheduler_list, container,false);
        ethr = (EditText) view.findViewById(R.id.ethr);
        etmin = (EditText) view.findViewById(R.id.etmin);
        etsec = (EditText) view.findViewById(R.id.etsec);
        alarm = (Button) view.findViewById(R.id.setAlarm);
        alarm.setOnClickListener(buttonalarmOnClickListener);


        RegisterAlarmBroadcast();

                return view; 
    }
}

问题1:未生成警报 我怀疑我没有正确注册广播接收器

  getActivity().registerReceiver(mReceiver, new IntentFilter("com.example.lightcontrolsystem.SchedulerFragment.class") );
        pendingIntent = PendingIntent.getBroadcast( getActivity(), 0, new Intent("sample"),0 );
        alarmManager = (AlarmManager)(getActivity().getSystemService( Context.ALARM_SERVICE ));

Problem2: EditText没有显示任何键盘或从PC上回复我的任何键。不知道为什么

提前致谢

1 个答案:

答案 0 :(得分:1)

为什么不尝试使用自定义BroadcastReceiver代替内联实现?试试这个

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Alarm time has been reached", Toast.LENGTH_LONG).show();
    }
}

AndroidManifest

中注册接收器
<receiver android:name=".AlarmReceiver" android:process=":remote"/>

在SchedulerListFragment.java中设置闹钟

Intent intent = new Intent(getActivity(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);

AlarmManager alarmManager = (AlarmManager)(getActivity().getSystemService( Context.ALARM_SERVICE ));
alarmManager.set( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + result , pendingIntent);

如果您愿意,可以通过传递空BroadcastReceiver,使用registerReceiver()动态注册IntentFilter。这也没有任何问题。