应用程序在创建Alarmobject - Alarmmanager时崩溃

时间:2016-06-16 18:41:16

标签: java android alarmmanager

我搜索了很多,但我无法找到我的具体问题的答案,所以我问自己的问题。

问题:

我正在编写应用程序,用户可以使用不同的重复间隔设置多个警报。但是当我创建一个Alarm对象时,应用程序崩溃了。

每次用户点击按钮时,都会调用Alarm类:

Alarm b = new Alarm(name, intervall, 23, 30, 15, 6, 2016);

这是我的Alarm课程:

package witty.de.witty;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;
import java.util.Calendar;

public class Alarm extends Activity {
    public Alarm(String name, int intervall, int stunden, int minuten, int tag, int monat, int jahr) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, jahr);
        cal.set(Calendar.MONTH, (monat - 1));
        cal.set(Calendar.DAY_OF_MONTH, tag);
        cal.set(Calendar.HOUR_OF_DAY, stunden);
        cal.set(Calendar.MINUTE, minuten);
        cal.set(Calendar.SECOND, 0);

        Intent myIntent = new Intent(Alarm.this, AlarmReceiver.class);

        final int _id = (int) System.currentTimeMillis();
        myIntent.setData(Uri.parse("reminder:" + _id));

        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, _id, myIntent, PendingIntent.FLAG_ONE_SHOT);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        alarmManager.setRepeating(AlarmManager.RTC, cal.getTimeInMillis(), intervall * AlarmManager.INTERVAL_DAY,
                pendingIntent);
    }
}

这是我的NotificationService课程:

package witty.de.witty;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;

import java.util.logging.Logger;

import witty.de.witty.MainActivity;
import witty.de.witty.R;

public class NotificationService extends Service {
    private NotificationManager mManager;

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);

        Toast mToast = Toast.makeText(
                this,
                "Reminders added to the calendar successfully for ",
                Toast.LENGTH_LONG);
        mToast.show();

        // Getting Notification Service
        mManager = (NotificationManager) this.getApplicationContext()
                .getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);

        Intent intent1 = new Intent(this.getApplicationContext(),
                MainActivity.class);

        intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
                | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
                this.getApplicationContext(), 0, intent1,
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("Witty2")
                        .setContentIntent(pendingNotificationIntent)
                        .setContentText("Errinerung!!");

        mManager.notify(0, mBuilder.build());
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }
}

这是我的AlarmReceiver课程:

package witty.de.witty;

import android.app.NotificationManager;
import android.content.Context;
import android.support.v4.app.NotificationCompat;
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) {
        Intent myIntent = new Intent(context, NotificationService.class);
        context.startService(myIntent);
    }
}

这是我的AndroidManifest文件:

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

    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <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"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Erstellen"></activity>
        <activity android:name=".Impressum"></activity>
        <activity android:name=".Startseite"></activity>
        <service android:name=".NotificationService"></service>
        <receiver android:name=".AlarmReceiver" android:process=":remote"></receiver>
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>

</manifest>

如果您需要更多代码或更多信息,请问我。

1 个答案:

答案 0 :(得分:0)

我的解决方案: 使用新的intent而不是Object来调用Alarm类:

Intent intent = new Intent(this, Alarm.class);
startActivity(intent);