What is Broadcast Receiver ?
A broadcast receiver (receiver) is an Android component which allows to register for system or application events.And All registered receivers for an event are notified by the Android runtime once this event happens.Android apps can send or receive broadcast messages from the Android system and other Android apps.A Broadcast Receiver is Runtime Issue in System and Notify System.
For Example. Your Mobile is Fully Charged or Low bettary or Downloaded File to Notify to send Broadcast in system apps. and display notification to low bettary etc.
Brodcast Receiver is Implement to add register in AndroidManifest file and You can also Dynamic add to broadcast receiver.
What is Alarm Manager ?
Android AlarmManager allows you to access system alarm.u can schedule your application to run at a specific time in the future. It works whether your phone is running or not.
Register Broadcast Receiver AndroidManifest.xml file
<receiver android:name=".AlarmReceiver" />
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.bhaumik.jobscheduledemo.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:layout_marginLeft="40dp" android:layout_marginRight="40dp" android:layout_marginTop="20dp" android:inputType="textPersonName" android:hint = "Enter Second " android:id="@+id/tv_time"/> <Button android:id="@+id/btn_startjob" android:layout_width="150dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="20dp" android:text="Start Alarm" android:textAllCaps="false" /> </LinearLayout>
MainActivity.javal
package com.example.bhaumik.jobscheduledemo; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Build; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { EditText time; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); time = findViewById(R.id.tv_time); jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);*/ findViewById(R.id.btn_startjob).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setAlarm(); Toast.makeText(MainActivity.this, "Alarm Set", Toast.LENGTH_SHORT).show(); } }); } private void setAlarm() { int sec = Integer.parseInt(time.getText().toString()); Intent intent=new Intent(MainActivity.this, AlarmReceiver.class); PendingIntent pendingIntent=PendingIntent.getBroadcast(getApplicationContext(),0, intent,0); AlarmManager a=(AlarmManager)getSystemService(ALARM_SERVICE); a.set(AlarmManager.RTC,System.currentTimeMillis() +sec*1000,pendingIntent); Toast.makeText(getApplicationContext(),"Alarm set",Toast.LENGTH_LONG).show(); } }
AlarmReceiver.java
package com.example.bhaumik.jobscheduledemo; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.media.RingtoneManager; import android.net.Uri; import android.support.v4.app.NotificationCompat; import android.widget.Toast; public class AlarmReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context,"Times up",Toast.LENGTH_LONG).show(); PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setContentTitle("Alarm Notify") .setContentText("This is Alarm Notification") .setSmallIcon(R.mipmap.ic_launcher) .setContentIntent(pendingIntent); NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(0,builder.build()); } }
0 comments:
Post a Comment