• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Difficulties on displaying another full screen activity for alarm clock app

Jayho

Lurker
Mar 20, 2017
3
0
I'm a student learning programming in Android Studio and I met some difficulties when developing my Alarm clock project. I've done lots of experiments and research in my code but still I couldn't find the solution. I wanted to try to make a normal alarm that is when the alarm is activated, display another full screen activity to remind the user. I don't want my alarm app to just ring and toast notification. I want it to also display a screen.

Here is my code:

Java:
import java.util.Calendar;

import static android.R.id.message;
import static android.provider.AlarmClock.EXTRA_MESSAGE;

public class MainActivity extends AppCompatActivity
{

    TimePicker alarmTimePicker;
    PendingIntent pendingIntent;
    AlarmManager alarmManager;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        alarmTimePicker = (TimePicker) findViewById(R.id.timePicker);
        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    }


    // Toggle button
    public void OnToggleClicked(View view)
    {
        long time;
        if (((ToggleButton) view).isChecked())
        {
            Toast.makeText(MainActivity.this, "Activated", Toast.LENGTH_SHORT).show();
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
            calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute() + 1);
            Intent intent = new Intent(this, AlarmReceiver.class);
            pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

            time=(calendar.getTimeInMillis()-(calendar.getTimeInMillis()%60000));
            if(System.currentTimeMillis()>time)

            {

                if (calendar.AM_PM == 0)
                    time = time + (1000*60*60*12);
                else
                    time = time + (1000*60*60*24);

            }


            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, 120000, pendingIntent);


        }
        else
        {

            Toast.makeText(MainActivity.this, "OFF", Toast.LENGTH_SHORT).show();
            alarmManager.cancel(pendingIntent);

        }
    }
}



And this is the manifest, not sure what I'm doing is right.Thank you for helping.


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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>
 
What does your AlarmReceiver class look like?

this is it

Java:
package com.example.android.exno11;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.widget.Toast;

import static android.R.attr.start;

public class AlarmReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        //put next activity

        Toast.makeText(context, "Wake up Now!", Toast.LENGTH_LONG).show();
        Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        if (alarmUri == null)
        {
            alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        }
        Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
        ringtone.play();
    }
}
 
Upvote 0
If you're wanting the AlarmReceiver to inflate one of your Activity Views, then this isn't possible unless the AlarmReceiver was created by your MainActivity class. In that case it would be executing in the Context of MainActivity.
As it stands, your BroadcastReceiver is being started by the system, so I don't see how it can inflate a specific Activity View. The best you can do in this context is to either use Log() or display a Toast message.
This is my understanding of how it works, if I'm wrong, quite happy to be corrected.
 
Upvote 0
If you're wanting the AlarmReceiver to inflate one of your Activity Views, then this isn't possible unless the AlarmReceiver was created by your MainActivity class. In that case it would be executing in the Context of MainActivity.
As it stands, your BroadcastReceiver is being started by the system, so I don't see how it can inflate a specific Activity View. The best you can do in this context is to either use Log() or display a Toast message.
This is my understanding of how it works, if I'm wrong, quite happy to be corrected.
Thank you for your reply :):)
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones