Sorry for my bad English. I faced with the problem. I try to create persistent alarm notification. Alarm notification must start every 10 seconds. I am using Alarm Manager,but it doest't work. What i am doing wrong?
public class RemindReceiver extends BroadcastReceiver {
private Class<?> activityClass;
public RemindReceiver() {
}
public RemindReceiver(Class<?> activityClass) {
this.activityClass = activityClass;
}
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.mipmap.ic_launcher, "Some Text", System.currentTimeMillis());
Intent intentTL = new Intent(context, activityClass);
notification.setLatestEventInfo(context, "Title", "Some Text",
PendingIntent.getActivity(context, 0, intentTL, PendingIntent.FLAG_CANCEL_CURRENT));
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
notifyManager.notify(1, notification);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(), 1000 * 5, pendingIntent);
}
public void setRemind(Context context) {
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, RemindReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(), 1000 * 5 , pendingIntent);
}}
Fragment:
public class PersonListFragment extends Fragment {
private RemindReceiver remindReceiver;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.person_list_fragment_layout, container, false);
Button nextButton = (Button) rootView.findViewById(R.id.next_button);
ListView personListView = (ListView) rootView.findViewById(R.id.name_list_view);
List<Person> personList = PersonListGenerator.generate();
PersonListAdapter adapter = new PersonListAdapter(getActivity(), personList);
personListView.setAdapter(adapter);
Context context = getActivity().getApplicationContext();
remindReceiver = new RemindReceiver(PersonListActivity.class);
remindReceiver.setRemind(context);
remindReceiver.onReceive(getActivity(), new Intent());
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ExpandablePersonListActivity.class);
startActivity(intent);
}
});
return rootView;
}}
And piece of my Android Manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<receiver android:name=".utility.RemindReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>