在Android中遇到触发警报的问题

时间:2016-07-07 07:29:23

标签: java android alarmmanager

我正在尝试在警报触发时创建通知。但是警报没有触发,也没有显示通知。这是MainActivity.java文件 -

params

这是AlarmReciever类 -

package com.kaushal28.kaushal1

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class MainActivity extends AppCompatActivity {



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

public void scheduleAlarm()
{

    Long time = new GregorianCalendar().getTimeInMillis()+10000;
    Intent intentAlarm = new Intent(this, AlarmReciever.class);

    AlarmManager alarmManager = (AlarmManager)  getSystemService(Context.ALARM_SERVICE);

    alarmManager.set(AlarmManager.RTC_WAKEUP,time, PendingIntent.getBroadcast(this,1,  intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
     Toast.makeText(this, "Alarm Scheduled", Toast.LENGTH_LONG).show();
    }
}

这是我的package com.kaushal28.kaushal1; 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.media.RingtoneManager; import android.net.Uri; public class AlarmReciever extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent1) { System.out.println("Notification setup"); Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); Intent intent=new Intent(context,MainActivity.class); PendingIntent pendingIntent=PendingIntent.getActivity(context,1,intent,0); Notification notification=new Notification.Builder(context) .setContentText("TESTING NOTIFIACTION") .setContentTitle("Notification") .setContentIntent(pendingIntent) .setSmallIcon(android.R.drawable.sym_def_app_icon) .setSound(sound) .build(); NotificationManager notificationManager= (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE) ; notificationManager.notify(1,notification); } } 文件:

menifest.xml

我在这里缺少什么?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

根据the docs,您需要添加清单文件中的接收器。

为简洁起见,我解释了相关的一些内容:

  

将接收器添加到您应用的清单文件中:

sudo gitlab-ctl reconfigure