Reputation: 43
I have an alarm clock app. When I create an alarm, it works well (If you do not exit the application), but if you exit, then the alarm does not work. I want the alarm to work even when the application is turned off and the phone is in sleep mode. Here is my code:
AlarmFragment (From which the alarm is set):
package org.vitaliy.numbell.Fragments;
public class AlarmFragment extends Fragment {
ImageButton add_btn;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_alarm, container, false);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
add_btn = view.findViewById(R.id.add_btn);
add_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar cur_time = Calendar.getInstance();
MaterialTimePicker materialTimePicker = new MaterialTimePicker.Builder()
.setTimeFormat(TimeFormat.CLOCK_24H)
.setHour(cur_time.get(Calendar.HOUR_OF_DAY))
.setMinute(cur_time.get(Calendar.MINUTE))
.build();
materialTimePicker.addOnPositiveButtonClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onClick(View view) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.MINUTE, materialTimePicker.getMinute());
calendar.set(Calendar.HOUR_OF_DAY, materialTimePicker.getHour());
//Set alarm clock
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(ALARM_SERVICE);
AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(calendar.getTimeInMillis(), getAlarmInfoPendingIntent());
alarmManager.setAlarmClock(alarmClockInfo, getAlarmActionPendingIntent());
}
});
materialTimePicker.show(getActivity().getSupportFragmentManager(),"tag_picker");
}
});
return view;
}
private PendingIntent getAlarmInfoPendingIntent() {
Intent alarmInfoIntent = new Intent(getActivity(), StartActivity.class);
alarmInfoIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
return PendingIntent.getActivity(getActivity(),0,alarmInfoIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}
private PendingIntent getAlarmActionPendingIntent() {
Intent intent = new Intent(getActivity(), AlarmActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
return PendingIntent.getActivity(getActivity(), 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
}
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.vitaliy.numbell">
<uses-permission android:name="android.permission.INTERNET" />
<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/Theme.Numbell">
<activity android:name=".AlarmActivity" />
<receiver
android:name=".Widget"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
</receiver>
<activity
android:name=".AppActivity"
android:configChanges="colorMode|keyboardHidden|screenSize|orientation|locale"
android:exported="false"
android:screenOrientation="portrait" />
<activity
android:name=".StartActivity"
android:configChanges="colorMode|keyboardHidden|screenSize|orientation|locale"
android:exported="true"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
AlarmActivity (Activity that opens when the alarm goes off):
package org.vitaliy.numbell;
public class AlarmActivity extends AppCompatActivity {
Ringtone ringtone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm);
WindowCompat.setDecorFitsSystemWindows(getWindow(), false);
Uri notificationUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
ringtone = RingtoneManager.getRingtone(this, notificationUri);
if(ringtone==null){
notificationUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
ringtone = RingtoneManager.getRingtone(this, notificationUri);
}
if(ringtone != null){
ringtone.play();
}
}
@Override
protected void onDestroy() {
if(ringtone != null && ringtone.isPlaying()){
ringtone.stop();
}
super.onDestroy();
}
}
Upvotes: 0
Views: 140
Reputation: 49
A better approach to this can be to show alarm via notifications (Broadcast Receiver) when app is not in background and when phone is locked or on standby you can call an activity there.
First off all add these two lines to Manifest to show activities on lockscreen:
<activity
android:name=".AppActivity"
android:configChanges="colorMode|keyboardHidden|screenSize|orientation|locale"
android:exported="false"
android:screenOrientation="portrait"
android:showOnLockScreen="true"
android:showWhenLocked="true" />
After this you can use KeyGuardManager and PowerManager to check in which state your phone is:
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if (pm.isInteractive() && pm.isScreenOn() && keyguardManager.isKeyguardLocked() && keyguardManager.isDeviceLocked()) {
// This will be helpful to show your activity or notification on lock screen
}
Upvotes: 0