Created
November 27, 2018 07:37
-
-
Save anandwana001/5d2eada9ab26bc25f115f2361cbc5f1e to your computer and use it in GitHub Desktop.
Alarm at Specific time open Activity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AlarmReceiver extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
// We're creating a new intent that's going to start the MainActivity | |
Intent scheduledIntent = new Intent(context, MainActivity.class); | |
scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
context.startActivity(scheduledIntent); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
private var alarmManager: AlarmManager? = null | |
private var pendingIntent: PendingIntent? = null | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
setupAlarm() | |
} | |
private fun setupAlarm() { | |
// AlarmManager instance from the system services | |
alarmManager = this.getSystemService(Context.ALARM_SERVICE) as AlarmManager | |
// Intent: this is responsible to prepare the android component what PendingIntent will start when the alarm is triggered. That component can be anyone (activity, service, broadcastReceiver, etc) | |
// Intent to start the Broadcast Receiver | |
val intent = Intent(this, AlarmReceiver::class.java) | |
// PendingIntent: this is the pending intent, which waits until the right time, to be called by AlarmManager | |
// The Pending Intent to pass in AlarmManager | |
pendingIntent = PendingIntent.getBroadcast(this.applicationContext, 0, intent, 0) | |
setAlarm() | |
} | |
private fun setAlarm() { | |
val calendar: Calendar = Calendar.getInstance().apply { | |
timeInMillis = System.currentTimeMillis() | |
set(Calendar.HOUR_OF_DAY, 13) | |
} | |
calendar.set(Calendar.MINUTE, 8) | |
alarmManager?.setInexactRepeating( | |
AlarmManager.RTC_WAKEUP, | |
calendar.timeInMillis, | |
AlarmManager.INTERVAL_DAY, | |
pendingIntent | |
) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<uses-permission android:name="android.permission.INTERNET" /> | |
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:roundIcon="@mipmap/ic_launcher_round" | |
android:supportsRtl="true" | |
android:theme="@style/AppTheme"> | |
<receiver android:name=".AlarmReceiver"/> | |
<activity android:name=".MainActivity" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment