通过AlarmManager发送通知

时间:2016-11-02 12:20:00

标签: java android android-service

我希望每一分钟都能推动一个通知并试一试: https://stackoverflow.com/a/8801990

但问题是,警报没有运行。 我试图设置日志,但我看不到它们......

现在我已经开始了一个新项目,这里是我的文件,也许一个导入是错误的,但我不知道......:

MainActivity.java

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //startService(new Intent(this, YourService.class));
        Log.e("MainActivity", "onCreate");
    }
}

Alarm.java

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.util.Log;
import android.widget.Toast;


public class Alarm extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
        wl.acquire();

        // Put here YOUR code.
        Log.e("Alarm", "onReceive");
        Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example

        wl.release();
    }

    public void setAlarm(Context context)
    {
        Log.e("Alarm", "setAlarm");
        AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, Alarm.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
        // Changed to 1 minute
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 1, pi); // Millisec * Second * Minute
    }

    public void cancelAlarm(Context context)
    {
        Intent intent = new Intent(context, Alarm.class);
        PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(sender);
    }
}

YourService.java

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class YourService extends Service
{
    Alarm alarm = new Alarm();
    public void onCreate()
    {
        Log.e("Service", "onCreate");
        super.onCreate();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        Log.e("Service", "onStartCommand");
        alarm.setAlarm(this);
        return START_STICKY;
    }
    @Override
    public void onStart(Intent intent, int startId)
    {
        alarm.setAlarm(this);
    }
    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }
}

它与链接中的代码相同,我只是将时间改为一分钟...... 这是我的Manifest.xml:

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

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

    <!--AutoStart Receiver -->
    <receiver android:name="com.example.alarm.alarmmanagertest.AutoStart">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"></action>
        </intent-filter>
    </receiver>

    <!-- Receiver and Permisson added -->
    <receiver android:process=":remote" android:name="com.example.alarm.alarmmanagertest.Alarm"></receiver>
    <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>

    <!-- Added my Service -->
    <service
        android:name=".YourService"
        android:enabled="true"
        android:process=":your_service" >
    </service>
</application>

Android监视器中没有任何内容,例如“E / Alarm:onReceive”,例如吐司也无法启动。 老实说,我尝试了所有方法,也在这里尝试过:http://it-ride.blogspot.de/2010/10/android-implementing-notification.html

有人可以帮助我,是否是推送通知的正确方法? 现在使用API​​ 16 ......

App Logcat:

11-02 21:48:19.234 3433-3433/? D/dalvikvm: Late-enabling CheckJNI
11-02 21:48:19.726 3433-3433/com.example.alarm.alarmmanagertest I/dalvikvm: Could not find method android.app.Application.registerOnProvideAssistDataListener, referenced from method com.android.tools.fd.runtime.BootstrapApplication.registerOnProvideAssistDataListener
11-02 21:48:19.726 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve virtual method 234: Landroid/app/Application;.registerOnProvideAssistDataListener (Landroid/app/Application$OnProvideAssistDataListener;)V
11-02 21:48:19.726 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
11-02 21:48:19.726 3433-3433/com.example.alarm.alarmmanagertest I/dalvikvm: Could not find method android.app.Application.unregisterOnProvideAssistDataListener, referenced from method com.android.tools.fd.runtime.BootstrapApplication.unregisterOnProvideAssistDataListener
11-02 21:48:19.726 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve virtual method 237: Landroid/app/Application;.unregisterOnProvideAssistDataListener (Landroid/app/Application$OnProvideAssistDataListener;)V
11-02 21:48:19.726 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
11-02 21:48:19.726 3433-3433/com.example.alarm.alarmmanagertest I/InstantRun: Instant Run Runtime started. Android package is com.example.alarm.alarmmanagertest, real application class is null.
11-02 21:48:19.734 3433-3433/com.example.alarm.alarmmanagertest W/InstantRun: No instant run dex files added to classpath
11-02 21:48:19.742 3433-3433/com.example.alarm.alarmmanagertest E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.monkeyPatchExistingResources
11-02 21:48:19.742 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve check-cast 1886 (Landroid/util/ArrayMap;) in Lcom/android/tools/fd/runtime/MonkeyPatcher;
11-02 21:48:19.742 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x1f at 0x025e
11-02 21:48:19.742 3433-3433/com.example.alarm.alarmmanagertest E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.pruneResourceCache
11-02 21:48:19.742 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve const-class 1886 (Landroid/util/ArrayMap;) in Lcom/android/tools/fd/runtime/MonkeyPatcher;
11-02 21:48:19.742 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x1c at 0x0060
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest I/dalvikvm: Could not find method android.view.Window$Callback.onProvideKeyboardShortcuts, referenced from method android.support.v7.view.WindowCallbackWrapper.onProvideKeyboardShortcuts
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve interface method 16034: Landroid/view/Window$Callback;.onProvideKeyboardShortcuts (Ljava/util/List;Landroid/view/Menu;I)V
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve interface method 16036: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve interface method 16040: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve virtual method 721: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve virtual method 743: Landroid/content/res/TypedArray;.getType (I)I
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x6e at 0x0008
11-02 21:48:20.437 3433-3433/com.example.alarm.alarmmanagertest I/dalvikvm: Could not find method android.widget.FrameLayout.startActionModeForChild, referenced from method android.support.v7.widget.ActionBarContainer.startActionModeForChild
11-02 21:48:20.437 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve virtual method 16467: Landroid/widget/FrameLayout;.startActionModeForChild (Landroid/view/View;Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
11-02 21:48:20.437 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x6f at 0x0002
11-02 21:48:20.453 3433-3433/com.example.alarm.alarmmanagertest I/dalvikvm: Could not find method android.content.Context.getColorStateList, referenced from method android.support.v7.content.res.AppCompatResources.getColorStateList
11-02 21:48:20.453 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve virtual method 449: Landroid/content/Context;.getColorStateList (I)Landroid/content/res/ColorStateList;
11-02 21:48:20.453 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
11-02 21:48:20.460 3433-3433/com.example.alarm.alarmmanagertest I/dalvikvm: Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawable
11-02 21:48:20.460 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve virtual method 684: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
11-02 21:48:20.460 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
11-02 21:48:20.460 3433-3433/com.example.alarm.alarmmanagertest I/dalvikvm: Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawableForDensity
11-02 21:48:20.460 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve virtual method 686: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
11-02 21:48:20.460 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
11-02 21:48:20.476 3433-3433/com.example.alarm.alarmmanagertest E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
11-02 21:48:20.476 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve instanceof 149 (Landroid/graphics/drawable/RippleDrawable;) in Landroid/support/v7/widget/AppCompatImageHelper;
11-02 21:48:20.476 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x20 at 0x000c
11-02 21:48:20.648 3433-3438/com.example.alarm.alarmmanagertest D/dalvikvm: GC_CONCURRENT freed 235K, 20% free 5386K/6664K, paused 5ms+6ms, total 32ms
11-02 21:48:20.656 3433-3433/com.example.alarm.alarmmanagertest E/MainActivity: onCreate
11-02 21:48:20.906 3433-3433/com.example.alarm.alarmmanagertest D/libEGL: loaded /system/lib/egl/libEGL_MRVL.so
11-02 21:48:20.960 3433-3433/com.example.alarm.alarmmanagertest D/libEGL: loaded /system/lib/egl/libGLESv1_CM_MRVL.so
11-02 21:48:20.976 3433-3433/com.example.alarm.alarmmanagertest D/libEGL: loaded /system/lib/egl/libGLESv2_MRVL.so
11-02 21:48:20.992 3433-3433/com.example.alarm.alarmmanagertest D/GC: <tid=3433> OES20 ===> GC Version   : GC Ver SS_rls_pxa988_JB42_R1_RC2_GC13.15 
11-02 21:48:21.046 3433-3433/com.example.alarm.alarmmanagertest D/OpenGLRenderer: Enabling debug mode 0

非App Logcat:

1-02 21:59:45.765 515-617/? V/AlarmManager: trigger ELAPSED_REALTIME_WAKEUP or RTC_WAKEUP
11-02 21:59:49.914 515-592/? I/Monitor: SIOP:: Current AP = 380, CP = 0, PST = 380
11-02 21:59:52.914 515-607/? D/BatteryService: update start
11-02 21:59:52.921 515-607/? D/BatteryService: level:100, scale:100, status:3, health:2, present:true, voltage: 4325, temperature: 220, technology: Li-ion, AC powered:false, USB powered:true, Wireless powered:false, icon:17303457, invalid charger:0, online:4, charge type:1, current avg:450
11-02 21:59:52.921 515-592/? D/BatteryService: Sending ACTION_BATTERY_CHANGED.
11-02 21:59:52.929 775-775/? D/STATUSBAR-BatteryController: onReceive() - ACTION_BATTERY_CHANGED
11-02 21:59:52.929 775-775/? D/STATUSBAR-BatteryController: onReceive() - BATTERY_STATUS_DISCHARGING: tw_stat_sys_battery_usb_not_charge
11-02 21:59:52.953 775-775/? D/STATUSBAR-IconMerger: checkOverflow(390), More:false, Req:false Child:13
11-02 21:59:52.968 775-775/? D/STATUSBAR-PhoneStatusBar:  mBrightnessEnablebySettings = true mBrightnessEnablebyBattery = true mBrightnessEnablebyDisableFlag = true
11-02 21:59:57.664 515-615/? I/PowerManagerService: [PWL] Off : 105s ago
11-02 21:59:59.921 515-592/? I/Monitor: SIOP:: Current AP = 380, CP = 0, PST = 380
11-02 21:59:59.992 515-617/? V/AlarmManager: waitForAlarm result :8
11-02 22:00:00.007 3817-3817/? D/KeyguardClockWidgetService: onReceive action=android.intent.action.TIME_TICK
11-02 22:00:00.015 515-592/? V/AlarmManager: ClockReceiver onReceive() ACTION_TIME_TICK
11-02 22:00:00.054 775-775/? D/STATUSBAR-IconMerger: checkOverflow(390), More:false, Req:false Child:13
11-02 22:00:03.015 515-607/? D/BatteryService: update start
11-02 22:00:05.039 515-595/? W/ProcessStats: Skipping unknown process pid 14477
11-02 22:00:05.039 515-595/? W/ProcessStats: Skipping unknown process pid 14479
11-02 22:00:05.039 515-595/? W/ProcessStats: Skipping unknown process pid 14480
11-02 22:00:09.929 515-592/? I/Monitor: SIOP:: Current AP = 380, CP = 0, PST = 380
11-02 22:00:12.062 515-837/? E/Watchdog: !@Sync 1050
11-02 22:00:12.078 515-617/? V/AlarmManager: waitForAlarm result :4

你可以看到,AlarmManager中有一些东西,但我认为它来自其他应用程序。我是否应该将我尝试使用AlarmManager的其他应用程序取消,然后重新启动设备?

2 个答案:

答案 0 :(得分:1)

<receiver>广播接收器清单中的Alarm块不正确。 name属性需要类的FQN或简写".Alarm",假设该类是清单文件顶部声明的包的一部分。

答案 1 :(得分:0)

所以,我尝试了这个,它终于奏效了!

http://stacktips.com/tutorials/android/repeat-alarm-example-in-android

因此,如果您想运行 AlarmManager并发送Notificiation ,通过单击它打开您的应用程序,这里是代码:

<强>的Manifest.xml

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

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

    <!-- Permission to start Alarm on device reboot -->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <activity
        android:name="alarmservice.javatechig.com.alarmservice.MyActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name="alarmservice.javatechig.com.alarmservice.AlarmReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>

    <!-- Will not be called unless the application explicitly enables it -->
    <receiver android:name="alarmservice.javatechig.com.alarmservice.DeviceBootReceiver"
        android:enabled="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>

</application>

<强> MyActivity.java

 import android.app.Activity;
 import android.app.AlarmManager;
 import android.app.PendingIntent;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.widget.Toast;
 import java.util.Calendar;

 public class MyActivity extends Activity {

 private PendingIntent pendingIntent;

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

    /* Retrieve a PendingIntent that will perform a broadcast */
    Intent alarmIntent = new Intent(MyActivity.this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(MyActivity.this, 0, alarmIntent, 0);

    findViewById(R.id.startAlarm).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            start();
        }
    });

    findViewById(R.id.stopAlarm).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cancel();
        }
    });

    findViewById(R.id.stopAlarmAt10).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startAt10();
        }
    });
}

public void start() {
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    int interval = 8000;

    manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
    Log.e("Alarm","started");
    Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}

public void cancel() {
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    manager.cancel(pendingIntent);
    Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
}

public void startAt10() {
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    int interval = 1000 * 60 * 20;

    /* Set the alarm to start at 10:30 AM */
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 13);
    calendar.set(Calendar.MINUTE, 27);

    /* Repeating on every 20 minutes interval */
    manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            1000 * 60 * 20, pendingIntent);
}

}

<强> DeviceBootReceiver.java

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;


/**
 * @author Neel
 *         <p/>
 *         Broadcast reciever, starts when the device gets starts.
 *         Start your repeating alarm here.
 */
public class DeviceBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            /* Setting the alarm here */

            Intent alarmIntent = new Intent(context, AlarmReceiver.class);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);

            AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

            int interval = 8000;

            manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);

            Toast.makeText(context, "Alarm Set", Toast.LENGTH_SHORT).show();
        }
    }
}

<强> AlarmReceiver.java

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;


public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        showNotification(context);
        System.out.println("running");
        Log.e("Alarm","running");

        // For our recurring task, we'll just display a message
        Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
    }

    private void showNotification(Context context) {
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, MyActivity.class), 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("My notification")
                        .setContentText("Finally");
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);
        NotificationManager mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());

    }
}

来源:http://stacktips.com/tutorials/android/repeat-alarm-example-in-android 一些提示:

  • <receiver android:name="">中写下全名
  • 不要忘记使用许可
  • 启动一个新项目,插入此代码并尝试解释其工作原理
  • 之后你可以投入你的项目