设置闹钟黑白间隔Android应用

时间:2018-07-06 16:12:21

标签: java android broadcastreceiver alarmmanager

我正在尝试构建一个应用程序,询问用户两个必须设置警报的特定时间!该应用程序还会向用户询问一个间隔[例如,如果用户将时间间隔设置为10分钟,并且时间是9:00和10:00,则该应用程序将每10分钟发出一次警报,黑白9和10。

该应用的问题是,时间到9:10时该应用就会崩溃.. 我的MainActivity.java文件是

package com.example.android.alarmfun;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.sql.Time;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

EditText initTimeEditText, finalTimeEditText, intervalTimeEditText;

AlarmManager alarmManager;
int requestCode = 0;


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

    initTimeEditText = findViewById(R.id.initTimeEditText);
    finalTimeEditText = findViewById(R.id.finalTimeEditText);
    intervalTimeEditText = findViewById(R.id.intervalTimeEditText);
    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);


}

public void setAlarm(View view) throws ParseException {

    Toast.makeText(MainActivity.this, "Alarm is On ", Toast.LENGTH_SHORT).show();
    DateFormat formatter = new SimpleDateFormat("HH:mm");
    String init_timeStamp = initTimeEditText.getText().toString();
    Time init_time = new Time(formatter.parse(init_timeStamp).getTime());
    String init_time_format = init_time.getHours() + ":" + init_time.getMinutes();

    String final_timeStamp = finalTimeEditText.getText().toString();
    Time final_time = new Time(formatter.parse(final_timeStamp).getTime());
    String final_time_format = final_time.getHours() + ":" + final_time.getMinutes();

    Log.i("Init Time", init_time_format);
    Log.i("Final Time", final_time_format);

    String intervals = intervalTimeEditText.getText().toString();


    int alarm_init_time = toMins(init_time_format);
    int alarm_final_time = toMins(final_time_format);
    int alarm_interval = toMins(intervals);

    Log.i("Alarm Init Time ", String.valueOf(alarm_init_time));
    Log.i("Alarm final time ", String.valueOf(alarm_final_time));
    Log.i("Alarm Interval ", String.valueOf(alarm_interval));

    ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();


    for (int i = alarm_init_time; i < alarm_final_time; ++i) {
        Intent intent = new Intent(MainActivity.this, AlarmReciever.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, requestCode, intent, 0);
        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + alarm_interval,
                pendingIntent);
        intentArray.add(pendingIntent);
        requestCode = requestCode + 1;
        alarm_init_time += alarm_interval;
    }


}

private static int toMins(String s) {
    String[] hourMin = s.split(":");
    int hour = Integer.parseInt(hourMin[0]);
    int mins = Integer.parseInt(hourMin[1]);
    int hoursInMins = hour * 60;
    return (hoursInMins + mins) * 60000;
}

}

我的AlarmReciever.java文件是

package com.example.android.alarmfun;

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

public class AlarmReciever extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Yay ! An alarm !", Toast.LENGTH_SHORT).show();
    }
}

我的activity_main.xml文件是

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    tools:context=".MainActivity">


    <EditText
        android:id="@+id/initTimeEditText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="Enter the Initial time in HH:MM format"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.034" />

    <EditText
        android:id="@+id/finalTimeEditText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="Enter the final time In HH:MM format"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/initTimeEditText" />

    <EditText
        android:id="@+id/intervalTimeEditText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="Enter the interval b/w alarm"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/finalTimeEditText" />

    <Button
        android:id="@+id/setAlarmButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:onClick="setAlarm"
        android:text="SET ALARM"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/intervalTimeEditText" />
</android.support.constraint.ConstraintLayout>

我的AndroidManifest.xml文件

 ?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.alarmfun">

    <uses-permission android:name="android.permission.SET_ALARM">

    </uses-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=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        </receiver>
    </application>

    </manifest>

我的logcat文件是

07-06 21:36:31.176 25764-25764/com.example.android.alarmfun I/Init Time: 9:37

07-06 21:36:31.176 25764-25764 / com.example.android.alarmfun I / Final时间:9:40 07-06 21:36:31.176 25764-25764 / com.example.android.alarmfun I / Alarm Init时间:34620000 07-06 21:36:31.176 25764-25764 / com.example.android.alarmfun I / Alarm最终时间:34800000 07-06 21:36:31.176 25764-25764 / com.example.android.alarmfun I / Alarm时间间隔:60000 07-06 21:37:30.077 25764-25775 / com.example.android.alarmfun I / zygote:后台并发复制GC已释放822(85KB)AllocSpace对象,0(0B)LOS对象,49%可用空间,4MB / 8MB,暂停563us共134.561ms 07-06 21:37:31.174 25764-25770 / com.example.android.alarmfun I / zygote:Thread [3,tid = 25770,WaitingInMainSignalCatcherLoop,Thread * = 0xa335ac00,peer = 0x13140bc0,“ Signal Catcher”]:信号3 07-06 21:37:31.210 25764-25770 / com.example.android.alarmfun I / zygote:将堆栈跟踪写入'/data/anr/traces.txt' 07-06 21:38:12.117 25764-25775 / com.example.android.alarmfun I / zygote:后台并发复制GC已释放830(53KB)AllocSpace对象,0(0B)LOS对象,49%可用空间,5MB / 11MB,暂停543us共171.169ms 07-06-21:38:26.130 25764-25775 / com.example.android.alarmfun I / zygote:后台并发复制GC释放了825(54KB)AllocSpace对象,0(0B)个LOS对象,49%的可用空间,5MB / 11MB,暂停560us总174.438ms 07-06 21:38:54.203 25764-25775 / com.example.android.alarmfun I / zygote:后台并发复制GC已释放847(53KB)AllocSpace对象,0(0B)LOS对象,46%可用空间,6MB / 12MB,暂停719us共130.046ms 07-06 21:39:11.342 25764-25764 / com.example.android.alarmfun E / AndroidRuntime:FATAL EXCEPTION:main     流程:com.example.android.alarmfun,PID:25764     DeadSystemException:系统死亡;较早的日志将指向根本原因

请帮帮忙!

0 个答案:

没有答案