BigBrownBear
BigBrownBear

Reputation: 25

Alarm Manager not woking

I am using AlarmManager on clicking floatingActionButton but it is not calling the alarm class. I tested the floatingActionButton via Toasts but it is working fine but Toast and vibrator in My_Alarm Class is not working even tough i have enebled permission in Manifests. Kindly point out the problem!

Below code main activity (Alarm Manager in floatingActionButton)

     floatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, AlarmManager.class);
            PendingIntent pendingIntent =   PendingIntent.getBroadcast(MainActivity.this,5,intent,0);
            sendBroadcast(intent);

            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+1000,pendingIntent);

        }
    });
}

This is my Alarm class (Toast and Vibrate not working)

public class My_Alarms extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Fuck This Shit", Toast.LENGTH_LONG).show();
    Vibrator v = (Vibrator)context.getSystemService(context.VIBRATOR_SERVICE);
    v.vibrate(10000);
}
}

And this is my Manifest file (I have enabled permission)

    <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/AppTheme">
    <activity android:name=".Activity_Time_Setting"
        android:grantUriPermissions="true"

        ></activity>
    <activity android:name=".MainActivity">
        android:grantUriPermissions="true"
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
            android:grantUriPermissions="true"
        </intent-filter>
    </activity>
    <receiver android:name=".My_Alarms"/>
</application>

Upvotes: 1

Views: 96

Answers (1)

haresh
haresh

Reputation: 1420

Replace this line of code :

Intent intent = new Intent(MainActivity.this, AlarmManager.class);

with this :

Intent intent = new Intent(MainActivity.this, My_Alarms.class);

Also you don't need to use

sendBroadcast(intent);

this line because you are already passing intent in pending intent

At last try to to remove toast from your broadcast reciver

Upvotes: 3

Related Questions