Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

implement alarm in monodroid

$
0
0

i want to implement alarm in my application. but i really don't know how to do it.
is there anyone to help me please?
i used the following codes and after running this code i just see "i'm running " in my screen and alarm is not fired
how can i solve this problem
i'm really confused.


[BroadcastReceiver]
class DeviceBootReceiver : BroadcastReceiver
{

public override void OnReceive(Context context, Intent intent) {

    if (intent.Action.Equals("android.intent.action.BOOT_COMPLETED"))
    {
        /* Setting the alarm here */
        Intent alarmIntent = new Intent(context, typeof( AlarmReceiver));
        PendingIntent pendingIntent = PendingIntent.GetBroadcast(context, 0, alarmIntent, 0);

        AlarmManager manager = (AlarmManager) context.GetSystemService(Context.AlarmService);
        int interval = 60000;

        manager.SetInexactRepeating(AlarmType.RtcWakeup, Java.Lang.JavaSystem.CurrentTimeMillis(), interval, pendingIntent);
      //  manager.SetInexactRepeating(AlarmType.RtcWakeup, Java.Lang.JavaSystem.CurrentTimeMillis(), AlarmManager.IntervalFifteenMinutes, pendingIntent);

        Toast.MakeText(context, "Alarm Setsds", ToastLength.Short).Show();
    }

}
}


[BroadcastReceiver]
class AlarmReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Toast.MakeText(context, "I'm running", ToastLength.Long).Show();
}
}


[Activity(Label = "@string/app_name", Icon = "@drawable/icon", MainLauncher = true)]
public class Activity1 : Activity
{
private PendingIntent pendingIntent;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Design);

    /* Retrieve a PendingIntent that will perform a broadcast */
    Intent alarmIntent = new Intent(this, typeof(AlarmReceiver));
    pendingIntent = PendingIntent.GetBroadcast(this, 0, alarmIntent, 0);

    Button StartServiceBtn = FindViewById<Button>(Resource.Id.startAlarm);
        StartServiceBtn.Click+=StartServiceBtn_Click;

}
private void StartServiceBtn_Click(object sender, EventArgs e)
{
start();
}

public void start() {

    AlarmManager manager = (AlarmManager) GetSystemService(Context.AlarmService);
    int interval = 60000; // 60000 = 1 min
    long test = SystemClock.CurrentThreadTimeMillis();
    long test2 = AlarmManager.IntervalFifteenMinutes;

    //  manager.SetInexactRepeating(AlarmType.ElapsedRealtimeWakeup, SystemClock.CurrentThreadTimeMillis(), interval, pendingIntent);
   // AlarmManager.IntervalFifteenMinutes

   manager.SetRepeating(AlarmType.RtcWakeup, Java.Lang.JavaSystem.CurrentTimeMillis(), interval , pendingIntent);
   //manager.Set(AlarmType.RtcWakeup, interval, pendingIntent);

   Toast.MakeText(this, "Alarm Set", ToastLength.Short).Show();
}

<?xml version="1.0" encoding="utf-8"?>

<application android:allowBackup="true" android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/AppTheme">

<activity android:name=".Activity1" android:label="@string/app_name">
</activity>

    <receiver android:name=".AlarmReceive" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <!-- Will not be called unless the application explicitly enables it -->
    <receiver android:name=".DeviceBootReceiver" android:enabled="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
</application>


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>