android - 我的 AlarmDemo 在我的 logcat 中不断崩溃,原因不明

标签 android broadcastreceiver alarmmanager repeatingalarm

我正在学习如何使用 AlarmManager 设置通知,但是当我运行它时,应用程序崩溃了,不知道为什么。谁能看一下它,看看有什么问题吗?

这是我的 MainActivity.java 文件

import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends Activity
{

 private PendingIntent pendingIntent;

@Override
public void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Calendar calendar = Calendar.getInstance();

    calendar.get(Calendar.MONTH);
    calendar.get(Calendar.YEAR);
    calendar.get(Calendar.DAY_OF_MONTH);

    calendar.set(Calendar.HOUR_OF_DAY, 14);
    calendar.set(Calendar.MINUTE, 50);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.AM_PM,Calendar.PM);

    Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);

} //end onCreate
}

这是我的 MyReceiver.java

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

public class MyReceiver extends BroadcastReceiver
{

@Override
public void onReceive(Context context, Intent intent)
{
    Intent service1 = new Intent(context, MyAlarmService.class);
    context.startService(service1);

}
}

这是我的 AlarmService.java 文件

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;


public class MyAlarmService extends Service
{

private NotificationManager mManager;

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

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

@SuppressWarnings("static-access")
@Override
public void onStart(Intent intent, int startId)
{
    super.onStart(intent, startId);

    mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);

    Notification notification;
    CharSequence from="AlarmDemo";
    CharSequence message="This is a test message!";
    //= new Notification(R.mipmap.ic_launcher,"This is a test message!", System.currentTimeMillis());
    intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
    Notification.Builder builder=new Notification.Builder(MyAlarmService.this);
    builder.setAutoCancel(false);
    builder.setContentTitle(from);
    builder.setContentText(message);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setContentIntent(pendingNotificationIntent);
    builder.build();
    notification=builder.getNotification();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    mManager.notify(0, notification);
}

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

}

这是我的 AndroidManifest.xml 文件

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

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

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".MyAlarmService"
        android:enabled="true" />

    <receiver android:name=".MyReceiver"/>
</application>

这是我的 logcat 所说的

09-10 15:09:36.259    7105-7129/com.example.androidalarmmanagerdemo E/eglCodecCommon﹕ writeFully: failed: Broken pipe
09-10 15:09:36.259    7105-7129/com.example.androidalarmmanagerdemo W/libEGL﹕ eglInitialize(0xa3788040) failed (EGL_SUCCESS)
09-10 15:09:36.259    7105-7129/com.example.androidalarmmanagerdemo I/OpenGLRenderer﹕ Initialized EGL, version 1.4
09-10 15:09:36.259    7105-7129/? E/EGL_emulation﹕ tid 7129: eglChooseConfig(533): error 0x3001 (EGL_NOT_INITIALIZED)
09-10 15:09:36.259    7105-7129/? W/OpenGLRenderer﹕ Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
09-10 15:09:36.259    7105-7129/? E/EGL_emulation﹕ tid 7129: eglChooseConfig(533): error 0x3001 (EGL_NOT_INITIALIZED)
09-10 15:09:36.259    7105-7129/? A/OpenGLRenderer﹕ Failed to choose config, error = EGL_NOT_INITIALIZED
09-10 15:09:36.259    7105-7129/? A/libc﹕ Fatal signal 4 (SIGILL), code 2,    fault addr 0xb74be62c in tid 7129 (RenderThread)

最佳答案

此致命信号在呈现应用程序时出现,并且似乎与您的代码无关。如果您在模拟器上运行,请尝试创建另一个模拟器,看看会发生什么。

关于android - 我的 AlarmDemo 在我的 logcat 中不断崩溃,原因不明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32509893/

相关文章:

android - 广播接收器不工作

android - 如何接收 RINGER_MODE_CHANGED_ACTION 的广播

android - 在 BroadcastReceiver 中使用 sqlite

android - 使用 collectInto 从 RxKotlin/RxJava 中的两个可观察源构建列表

android - 手机间隙 : Source URL is not in Whitelist

android - 通过 Backendless 中的 API 在 GeoPoint 半径范围内搜索,从用户表中检索用户名列表

java - Firestore SetOptions.mergeFields 不存储字段

android - 为什么我的 AlarmManager 会立即启动?

android - onPostExecute 未在 AsyncTask 中调用(处理程序运行时异常)

android - 在特定时间重复的 AlarmManager