我正在创建一个具有重复警报的应用程序,它会检查来自api的日期,如果“日期”为真,它将发送通知。注释掉的通知就是我正在使用的,因为它在My MainActivity类中工作,我只是无法在我的Alarm类中通知它。有什么想法吗?
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();
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
try {
request.setURI(new URI("exampleTimeUrl"));
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
String responseText = EntityUtils.toString(entity);
JSONObject jsonObj = new JSONObject(responseText);
String istheday = jsonObj.getString("birthday");
if (istheday=="true"){
/// notify
/*
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify = new Notification(android.R.drawable.stat_notify_more, "Happy Birthday", System.currentTimeMillis());
Context contextA = MainActivity.this;
CharSequence title = "happy birthday";
CharSequence details = "yay";
Intent intent2 = new Intent(contextA, MainActivity.class);
PendingIntent pending = PendingIntent.getActivity(contextA, 0, intent2, 0);
notify.setLatestEventInfo(contextA, title, details, pending);
nm.notify(0, notify);
*/
/// end of notify
}
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// end your code here
wl.release();
}
public void SetAlarm(Context context)
{
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 8000, pi); // Millisec * Second * Minute - will obviously increase this to run every day
}
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);
}
}
答案 0 :(得分:2)
检查一下:
Notification notification;
NotificationManager nm;
NotificationManager mNotificationManager;
NotificationCompat.Builder mBuilder;
private static final int NOTIFICATION_ID = 1;
@Override
public void onReceive(Context context, Intent arg1) {
showNotification(context);
}
private void showNotification(Context context) {
Uri alarmSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (alarmSound == null) {
alarmSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.image)
.setSound(alarmSound)
.setContentTitle("New Locations Found")
.setContentText(
value
+ " New Locations has been assigned to you");
mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
mBuilder.setAutoCancel(true);
Intent resultIntent = new Intent(this, HeliosActivity.class);
resultIntent.putExtra("skiphomescreen", true);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(HeliosActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}