0
Я делаю приложение с повторяющимся сигналом тревоги, которое проверяет дату с api, если «дата» истинна, он отправит уведомление. Отмеченное уведомление - это то, с чем я работаю, поскольку он работает в классе My MainActivity, я просто не могу получить его уведомление в моем классе Alarm. Есть идеи?Android уведомляет меня о своем классе тревоги
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);
}
}
извините, это мое первое приложение, я разделить эти методы? какую часть я вставляю в свое заявление if? – rolandnsharp
вызовите 'showNotification (context);' в ваш оператор if – ryderz8
, потому что я получаю ошибку IDE «void является недопустимым типом для переменной onReceive», когда я помещаю эту первую часть в stat if – rolandnsharp