报警未设置

时间:2014-09-27 04:00:37

标签: java android android-notifications android-alarms

我使用切换按钮来设置警报,该警报调用另一个包中的类来添加必要的信息。有一个将被调用的Service类。但通知不会来。

继承我的代码:

int mYear = Integer.parseInt(formattedYear);
                    int mMonth = Integer.parseInt(formattedMonth);
                    int mDay = Integer.parseInt(formattedDay) - 1; // 1 day before the exact day
                    int mHour = Integer.parseInt(formattedHour);
                    int mMin = Integer.parseInt(formattedMin);


                    SetAlarm sa = new SetAlarm(context, mYear, mMonth, mDay, mHour, mMin, title, body, iD);
                    sa.startAlarm();


public class SetAlarm {
    private Context context;
    private PendingIntent mAlarmSender;
    private int mYear;
    private int mMonth;
    private int mDay;
    private int mHour;
    private int mMin;
    private int iD;
    String title;
    String body;

    public SetAlarm(Context context, int mYear, int mMonth, int mDay, int mHour, int mMin, String title, String body, int iD) {
         this.context = context;
         this.mYear = mYear;
         this.mMonth = mMonth;
         this.mDay = mDay;
         this.mHour = mHour;
         this.mMin = mMin;
         this.title = title;
         this.body = body;
         this.iD =iD;

    }

    public void startAlarm(){
        Intent intent = new Intent();
        //Set the alarm to 10 seconds from now
        Calendar c = Calendar.getInstance();

        Log.d("Check", mYear+"/"+mMonth+"/"+mDay+"/"+mHour+"/"+mMin);
        c.add(Calendar.YEAR, mYear);
        c.add(Calendar.MONTH, mMonth);
        c.add(Calendar.DAY_OF_MONTH, mDay);
        c.add(Calendar.HOUR_OF_DAY, mHour);
        c.add(Calendar.MINUTE, mMin);
        c.add(Calendar.SECOND, 00);

        // Schedule the alarm!
        AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

        intent.setClass(context, com.example.alarm.helper.AlarmReceiver.class);


        mAlarmSender = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);


        am.set(AlarmManager.RTC_WAKEUP,  c.getTimeInMillis() , mAlarmSender);
        }
    }

服务类是:

public class AlarmReceiver extends Service {

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

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

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

    @SuppressWarnings("deprecation")
    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        Intent notificationIntent = new Intent(this , MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        int icon = R.drawable.ic_notification;
        String tickerText = "Reminder";
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, tickerText, when);
        String contentTitle = "Title";
        String contentText = "Text";
        notification.setLatestEventInfo(this, contentTitle, contentText, pendingIntent );
        notificationManager.notify(123, notification);

        Toast.makeText(this, "Reminder On", Toast.LENGTH_SHORT).show();

    }
}

Android Manifest:

<service android:name="com.example.alarm.helper.AlarmReceiver"
         android:enabled="true">
     </service>

1 个答案:

答案 0 :(得分:0)

Intent intentsOpen = new Intent(this, AlarmReceiver.class);
intentsOpen.setAction("set any name of action");
pendingIntent = PendingIntent.getBroadcast(this, 111, intentsOpen, 0);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
            c.getTimeInMillis(), "set interval time in mili second", pendingIntent); // if you setting 1000 milisecond then notification is send after every 1000 milisecond
在AlarmReceiver.class中

public class AlarmReceiver extends BroadcastReceiver {
private final String SOMEACTION = "set any name of action";

@Override
public void onReceive(Context context, Intent intent) {
    generateNotification(context,"Hi how are you?");
    String action = intent.getAction();
    if (SOMEACTION.equals(action)) {
        //do what you want here
        generateNotification(context,"Hi how are you?");
    }
}

@SuppressWarnings("deprecation")
private void generateNotification(Context context, String message) {
      System.out.println(message+"++++++++++2");
      int icon = R.drawable.ic_launcher;
      long when = System.currentTimeMillis();
      NotificationManager notificationManager = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);
      Notification notification = new Notification(icon, message, when);
      String title = context.getString(R.string.app_name);
      String subTitle = context.getString(R.string.app_name);
      Intent notificationIntent = new Intent(context, OutPut.class);
      notificationIntent.putExtra("content", message);
      PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
      notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
        | Intent.FLAG_ACTIVITY_SINGLE_TOP);

      notification.setLatestEventInfo(context, title, subTitle, intent);
      //To play the default sound with your notification:
      notification.defaults |= Notification.DEFAULT_SOUND;
      notification.flags |= Notification.FLAG_AUTO_CANCEL;
      notification.defaults |= Notification.DEFAULT_VIBRATE;
      notificationManager.notify(0, notification);



}

}
清单文件中的

  <receiver android:name=".AlarmReceiver" >
        <intent-filter>
            <action android:name="set any name of action" />
        </intent-filter>
    </receiver>
</application>
相关问题