如何在android中使用后台进程和报警管理器创建通知消息?

时间:2013-12-17 05:38:11

标签: android notifications alarmmanager intentservice

我一直在苦苦挣扎几天,我无法理解如何使用后台服务在常规时间段(即!分钟)显示通知栏中的通知我看到很多代码而且我完全是困惑何时使用什么。任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

首先为火警警报管理器创建一个按钮 -

@覆盖

public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v==btnStartAlarm){
            fireAlarm();
        }

这是我的闹钟管理器 -

public void fireAlarm() {
        /**
         * call broadcost reciver
         */
        Intent intent = new Intent(context, AlarmReceiver.class);
        intent.setAction("com.manish.alarm.ACTION");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 111, intent,0);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        AlarmManager alarm = (AlarmManager) MainActivity.this.getSystemService(Context.ALARM_SERVICE);
        alarm.cancel(pendingIntent);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),100000, pendingIntent);
    }

现在创建一个BroadcastReceiver类并触发通知方法 -

public class AlarmReceiver extends BroadcastReceiver {
    private final String SOMEACTION = "com.manish.alarm.ACTION";

    @Override
    public void onReceive(Context context, Intent intent) {

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



    }

}

创建显示通知消息的类 -

public class OutPut extends Activity {
    TextView textmessage;
    String stringValue;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_output);

        textmessage = (TextView) findViewById(R.id.textView1);

        Intent intent = getIntent();
        stringValue = intent.getStringExtra("content");
        textmessage.setText(stringValue);
        System.out.println(stringValue);

    }
}

在manifest.xml中,只需要获得振动许可 -

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

这里从github-下载链接 http://www.androidhub4you.com/2013/12/android-alarm-manager-example-how-to.html

https://github.com/manishsri01/AndroidAlarmManagerDemo