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: mit einem Alarm eine Activity öffnen

01.01.2021 (👁6785)


 

Android Code Beispiel:

Mit diesem Code-Beispiel werden in einer Android App ein Alarm erstellt wie bei einem Wecker und dieser öffnet beim Auslösen des Alarms eine Activity Seite

 

 

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 >------------
   
private AlarmManager alarmManager  = null;
   
private PendingIntent pendingIntent  = null;

   
@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() >--------
        // AlarmManager instance from the system services
       
alarmManager = (AlarmManager)   this.getSystemService(Context.ALARM_SERVICE);

       
// Intent: this is responsible to prepare the android component what PendingIntent will start when the alarm is triggered. That component can be anyone (activity, service, broadcastReceiver, etc)
        // Intent to start the Broadcast Receiver
       
Intent intent =new  Intent(this, AlarmReceiver.class);

       
// PendingIntent: this is the pending intent, which waits until the right time, to be called by AlarmManager
        // The Pending Intent to pass in AlarmManager
       
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

       
//< create Alarm >
       
setAlarm();
       
//</ create Alarm >
        //--------</ setupAlarm() >--------
   
}
   
   
private void setAlarm() {
       
//--------< setup() >--------
       
AlarmManager am = (AlarmManager)   this.getSystemService(Context.ALARM_SERVICE);
       
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ 2000, pendingIntent);
       
//--------</ setup() >--------
   
}
   
//========</ Methods >===========
    //------------</ MainActivity >------------
}

 

 

 

 

 

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">


    <
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" />
    </
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>