不使用此代码(警报管理器)

时间:2015-04-11 09:05:55

标签: android service

我已添加了所有必需的访问权限,但此代码无效。

Calendar calendarAlarm = Calendar.getInstance();
    Calendar c = Calendar.getInstance();
    calendarAlarm.set(Calendar.YEAR, c.get(calendarAlarm.YEAR));
    calendarAlarm.set(Calendar.MONTH, c.get(calendarAlarm.MONTH));
    calendarAlarm.set(Calendar.DAY_OF_MONTH,c.get(calendarAlarm.DAY_OF_MONTH));
    calendarAlarm.set(Calendar.HOUR, 9);
    calendarAlarm.set(Calendar.MINUTE, 30);
    calendarAlarm.set(Calendar.SECOND, 0);

    Cursor cursorAlarm=G.database.rawQuery("SELECT * FROM gharz WHERE date2 LIKE '%"+dateFarsi2+"%' AND done='false'", null);
    if(cursorAlarm.getCount()!=0){
    Intent intent = new Intent(G.context, MyReceiver.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(G.context, 0, intent, 0);
    G.alarmManager.set(AlarmManager.RTC_WAKEUP, calendarAlarm.getTimeInMillis(), pendingIntent);
    Log.i("tag", "if main activity");
    }

public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Cursor cursorAlarm=G.database.rawQuery("SELECT * FROM gharz WHERE date2 LIKE '%"+MainActivity.dateFarsi2+"%' AND done='false'", null);
    if(cursorAlarm.getCount()!=0){
        Intent service1 = new Intent(context, MyAlarmService.class);
        context.startService(service1);
        Log.i("tag", "if my service");
    }
}
}

服务类

public class MyAlarmService extends Service {

private NotificationManager mManager;

@Override
public IBinder onBind(Intent arg0)
{
    return null;
}

@Override
public void onCreate()
{
    super.onCreate();
}

@SuppressWarnings("static-access")
@Override
public void onStart(Intent intent, int startId)
{
    super.onStart(intent, startId);

    mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(this.getApplicationContext(), MainActivity.class);

    Notification notification = new Notification(R.drawable.icon1, "message!", System.currentTimeMillis());
    intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.setLatestEventInfo(this.getApplicationContext(), "test", "test", pendingNotificationIntent);
    Toast.makeText(this, "test", Toast.LENGTH_LONG).show();
    MakeSomeSound();
    mManager.notify(0, notification);
}

public void MakeSomeSound() {
    try {
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
        r.play();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

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

另一个问题是代码的结构是否正确

0 个答案:

没有答案