Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Android: Eine App per Alarm öffnen auch aus LockScreen

02.01.2021 (πŸ‘224)

Android: mit to open an alarm an activity even in locked screen

 

Android Code Example:

This code example creates an alarm in an Android app like an alarm clock and it opens an activity page when the alarm is triggered.

The App even opens the app activity, when the smartphone is locked in LockScreen.

 

 

 

MainActivity.java

 

package com.example.demowakeupactivity;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
   
//------------< MainActivity >------------

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(
R.layout.activity_main);
    }

   
//========< Buttons >===========
   
public void btnOpenWakeup(View view) {
       
Intent openWakeup = new Intent(this, WakeupAlarmActivity.class);
        startActivity(
openWakeup);
    }
   
public void btnAlarmSetWakeup(View view) {
       
//--------< btnAlarmSetWakeup() >--------
       
setupAlarm();
       
//--------</ btnAlarmSetWakeup() >--------
   
}
   
//========</ Buttons >===========


    //========< Methods >===========

   
private void setupAlarm() {
       
//--------< setupAlarm() >--------

        //*Create an Alarm Receiver, which waits and listens for an alarm event
       
Intent intentAlarmReceiver =new  Intent(this, AlarmReceiver.class);

       
//*Send Alarmreceiver to Broadcast, like global hook.
       
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intentAlarmReceiver, 0);

       
//--------< setup() >--------
        //AlarmManager instance from the system services
       
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
       
//set time for alarm and attach to broadcast
       
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ 2000, pendingIntent);
       
//--------</ setup() >--------
        //--------</ setupAlarm() >--------
   
}

   
//========</ Methods >===========
    //------------</ MainActivity >------------
}

 

 

WakeupAlarmActivity.java

The Activity Page, which will be opend by the alarm

package com.example.demowakeupactivity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.TargetApi;
import android.app.KeyguardManager;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class WakeupAlarmActivity extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(
R.layout.activity_wakeup);



       
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {

            setShowWhenLocked(
true);
            setTurnScreenOn(
true);
           
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(this.KEYGUARD_SERVICE);
           
keyguardManager.requestDismissKeyguard(this, null);
        }
       
else {

            getWindow().addFlags(
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                   
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                   
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
                   
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
    }
}

 

 

 

 

AlarmReceiver.java

package com.example.demowakeupactivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AlarmReceiver extends BroadcastReceiver {

   
@Override
   
public void onReceive(Context context, Intent intent) {
       
//------< onReceive() >--------
        //this event triggers when alarm in Android happens
        //*open Wakeup Alarm Activity
       
Intent alarmIntent = new Intent(context, WakeupAlarmActivity.class);
       
alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       
//open Activitiy
       
context.startActivity(alarmIntent);
       
//------</ onReceive() >--------
   
}
}

 

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   
package="com.example.demowakeupactivity">

    <
uses-permission android:name="android.permission.WAKE_LOCK" />
    <
uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <
uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
    <
uses-permission android:name="android.permission.ACTION_MANAGE_OVERLAY_PERMISSION" />
    <
uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <
uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />


    <
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/Theme.DemoWakeupActivity">

        <
receiver android:name=".AlarmReceiver"></receiver>

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

        <
activity

            
android:name=".WakeupAlarmActivity"
           
android:parentActivityName=".MainActivity"
           
android:excludeFromRecents="true"
           
android:showOnLockScreen="true"
           
android:showWhenLocked="true"
           
android:turnScreenOn="true"
           
android:showForAllUsers="true"

           
/>
    </
application>


</
manifest>

 

 

Activities sind standard.

 

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
tools:context=".MainActivity"
   
android:orientation="vertical"
   
>


    <
Button
       
android:id="@+id/btnOpenWakeup"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:backgroundTint="#A1A0A0"
       
android:onClick="btnOpenWakeup"

       
android:text="Open Wakeup" />

    <
Button
       
android:id="@+id/btnWakeup"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:backgroundTint="#FFEB3B"
       
android:onClick="btnAlarmSetWakeup"
       
android:text="Alarm: Wakeup"
       
android:textColor="#F44336" />

</
LinearLayout>

 

 

 

Und eine Alarm Activity

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:background="#FF0000"
   
tools:context=".WakeupAlarmActivity">

    <
TextView
       
android:id="@+id/textView"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Alarm"
       
android:textColor="#FFEB3B"
       
android:textSize="36sp"
       
android:textStyle="bold"
       
tools:layout_editor_absoluteX="155dp"
       
tools:layout_editor_absoluteY="292dp" />
</
androidx.constraintlayout.widget.ConstraintLayout>