我正在制作一个警报应用程序,它将通知用户提供的时间。
我从我的片段MyAlarm
调用了一个类TimepickerFragment
,但它没有传递给MyAlarm类。
以下是我片段中的代码:
public class TimepickerFragment extends Fragment {
Button SetTime;
TimePicker timePicker;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.timepickerfragment,null);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
timePicker = (TimePicker) view.findViewById(R.id.timePicker);
SetTime = (Button) view.findViewById(R.id.settime);
SetTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar calendar = Calendar.getInstance();
calendar.set(
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH),
timePicker.getCurrentHour(),
timePicker.getCurrentMinute(),
0
);
setAlarm(calendar.getTimeInMillis());
Fragment fragment = null;
fragment = new Room1Fragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.screen_area, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
private void setAlarm(long timeInMillis) {
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent (getActivity(), MyAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0 , intent, 0);
alarmManager.setRepeating(AlarmManager.RTC , timeInMillis, AlarmManager.INTERVAL_DAY, pendingIntent);
Toast.makeText(getActivity(), "Alarm is set", Toast.LENGTH_SHORT).show();
}
});
}
}
它应该传递给这个类:
public class MyAlarm extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "MyAlarm Working", Toast.LENGTH_SHORT).show();
intent = new Intent(context, MyService.class);
context.startService(intent);
}
}
这是我的清单文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.haziqnaeem.settimer2">
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyAlarm"
android:enabled="true"
android:exported="true" />
<service
android:name=".MyService"
android:enabled="true"
android:exported="true"></service>
</application>
需要帮助
答案 0 :(得分:0)
您还必须在清单文件中声明您的MyAlarm
类,如下所示
<receiver
android:name=".MyAlarm"
android:process=":remote" />
具有以下权限
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.VIBRATE"/>
答案 1 :(得分:0)
如果我理解你很好,你想要的是,时间到了,你的设备会向用户发出警报,但是类MyAlarm它是一个WakefulBroadcastReceiver,它是:“实现接收设备唤醒事件的BroadcastReceiver,然后将工作传递给服务,同时确保设备在过渡期间不会重新入睡“==&gt;来自官方文档。
现在,您已经设置了AlarmManager,因此,在pendingIntent午餐中,一个例如Intent服务的例子将触发声音或通知或类似的东西,但是知道AlarmManager实际上不会为您打开屏幕,因此不会调用WakefulBroadcastReceiver,您可以使用带有唤醒锁的PowerManager。 并且还要知道WakefulBroadcastReceiver在API级别26.1.0中已被弃用。