Search code examples
androidbroadcastreceiveralarmmanagerandroid-pendingintent

Android - Alarm Manager not working?


I am trying to trigger an alarm when clicking the button. But it is not working what is the problem here.

AlarmSet.java

private void setAlarm() {   
    //Alarm 
    Toast.makeText(getApplicationContext(), "alerm set", Toast.LENGTH_LONG).show();


    Intent myIntent = new Intent(this, AlarmManager.class); PendingIntent        
    pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent,0);

   AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
   alarmManager.set(AlarmManager.RTC, (System.currentTimeMillis(), pendingIntent);

    //Alarm END
}

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {

    System.out.println("ALARM SUCCESS");
    // TODO Auto-generated method stub
    Toast.makeText(context, "Don't panik but your time is up!!!!.",
            Toast.LENGTH_LONG).show();
    // Vibrate the mobile phone
    Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    vibrator.vibrate(2000);

    Intent service1 = new Intent(context, MyAlarmService.class);
    context.startService(service1);
} 
}

MyAlarmService.java

public class MyAlarmService extends Service 

{
private NotificationManager mManager;

@Override
public IBinder onBind(Intent arg0)
{
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() 
{
    // TODO Auto-generated method stub  
    super.onCreate();
}

@SuppressWarnings({ "static-access", "deprecation" })
@Override
public void onStart(Intent intent, int startId)
{
    super.onStart(intent, startId);

    mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(this.getApplicationContext(),CurrentParking.class);

    Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", System.currentTimeMillis());

    intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.setLatestEventInfo(this.getApplicationContext(), "Sweeper Stopper", "This is a test message!", pendingNotificationIntent);

    mManager.notify(0, notification);
}

@Override
public void onDestroy() 
{
    // TODO Auto-generated method stub
    super.onDestroy();
}

}

Manifest

  <application
    android:allowBackup="true"
    android:icon="@drawable/logo"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Black.NoTitleBar" >

    <service
        android:name="com.menthatech.sweeperstopper.MyAlarmService"
        android:enabled="true" />

    <receiver
        android:name="com.menthatech.sweeperstopper.AlarmReceiver"
        android:enabled="true"
        android:process=":remote" />

</application>

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg"
tools:context=".LandingPage" >

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/footer" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <EditText
            android:id="@+id/et_address"
            android:layout_width="fill_parent"
            android:layout_height="80dp"
            android:layout_marginTop="15dp"
            android:background="@drawable/edittext_background"
            android:gravity="center"
            android:inputType="textMultiLine"
            android:textColor="@android:color/white"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/et_datefield"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_below="@+id/et_address"
            android:layout_marginTop="15dp"
            android:background="@drawable/edittext_background"
            android:focusable="false"
            android:gravity="center"
            android:textColor="@android:color/white"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/et_sweepingTime"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_below="@+id/et_datefield"
            android:layout_marginTop="15dp"
            android:background="@drawable/edittext_background"
            android:focusable="false"
            android:gravity="center"
            android:textColor="@android:color/white"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/et_alertTime"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_below="@+id/et_sweepingTime"
            android:layout_marginTop="15dp"
            android:background="@drawable/edittext_background"
            android:focusable="false"
            android:gravity="center"
            android:textColor="@android:color/white"
            android:textStyle="bold" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/et_alertTime"
            android:layout_gravity="center"
            android:layout_marginTop="15dp"
            android:gravity="center"
            android:orientation="vertical" >

            <Button
                android:id="@+id/checkForSchedule"
                android:layout_width="fill_parent"
                android:layout_height="40dp"
                android:layout_gravity="center"
                android:background="@drawable/edittext_background"
                android:gravity="center"
                android:text="Check For The Schedule"
                android:textColor="@android:color/white"
                android:textSize="18sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/setAlarm"
                android:layout_width="100dp"
                android:layout_height="40dp"
                android:layout_gravity="center"
                android:layout_marginTop="15dp"
                android:background="@drawable/edittext_background"
                android:gravity="center"
                android:text="Alarm"
                android:textColor="@android:color/white"
                android:textSize="18sp"
                android:textStyle="bold" />
        </LinearLayout>
    </RelativeLayout>
</ScrollView>

<LinearLayout
    android:id="@+id/footer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/home" />

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/hometext" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/park" />

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/parktext" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/mycar" />

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/mycartext" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_settings"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/settings" />

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/settingstext" />
    </LinearLayout>
</LinearLayout>

</RelativeLayout>

Solution

  • Intent myIntent = new Intent(this, AlarmManager.class);
    

    In this line change AlarmManager.class to AlarmReceiver.class