0

I apologies in advance if I'm not good in writing English. I'm writing a simple task app that remind me with alarm in specific time.

Below I set alarm with AlarmManager :

private static void setAlarm(Context context, Calendar calendar,
        PendingIntent pIntent) {
    AlarmManager alarmManager = (AlarmManager) context
        .getSystemService(Context.ALARM_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= 
            android.os.Build.VERSION_CODES.KITKAT) {
        alarmManager.setExact(AlarmManager.RTC_WAKEUP,
            calendar.getTimeInMillis(), pIntent);
    } else {
        alarmManager.set(AlarmManager.RTC_WAKEUP,
            calendar.getTimeInMillis(), pIntent);
    }
}

and then AlarmManagerHelper :

public class AlarmManagerHelper extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        String title = intent.getStringExtra("Title");
        int hour = intent.getIntExtra("Hour", 0);
        int min = intent.getIntExtra("Minute", 0);
        String alarmTone = intent.getStringExtra("AlarmTone");
        Intent i = new Intent();
        i.setClassName("com.example.tasks",
            "com.example.tasks.AlarmScreenActivity");
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.putExtra("Title", title);
        i.putExtra("Hour", hour);
        i.putExtra("Minute", min);
        i.putExtra("AlarmTone", alarmTone);
        context.startActivity(i);
    }
}

and AlarmScreenActivity is:

public class AlarmScreenActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // get intent
        pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
        wl.acquire();
        mPlayer = new MediaPlayer();
        try {
            if (task_Tone != null && !task_Tone.equals("")) {
                android.net.Uri toneUri = android.net.Uri.parse(task_Tone);
                if (toneUri != null) {
                    mPlayer.setDataSource(this, toneUri);
                    mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                    mPlayer.setLooping(true);
                    mPlayer.prepare();
                    mPlayer.start();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // some code
    public void onClickDissmis(View view) {
        mPlayer.stop();
        finish();
    }

    protected void onDestroy() {
        super.onDestroy();
        wl.release();
    }
}

then with AlarmManagerHelper and AlarmScreenActivity displaying it.

my problem is: in the specific time that should wake up and ringing not do int, so when I press power button an turn screen on that is work???! (when is in debug mode and the device , connected to system work properly) I hope that describe my problem perfectly.

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
AsmA
  • 29
  • 1
  • 5

2 Answers2

0

I don't understand your problem, exactly. I can say, though, that Android only guarantees that it is holding a wakelock while it delivers the Broadcast. Your code leave considerable time between the reception of the Broadcast, by the Receiver, and the time you seize the wakelock. There is nothing to prevent the device from going back to sleep, in that interval.

G. Blake Meike
  • 6,615
  • 3
  • 24
  • 40
  • thanks from your answer...in onclick in a button in that AlarmScreenActivity like this:'public void onClickDissmis(View view){ mPlayer.stop(); finish(); }'I think that I should put below code – AsmA Jan 12 '15 at 17:19
  • I edited my post... hope that described better...sorry for that. – AsmA Jan 12 '15 at 17:48
0

While AlarmManagerHelper.onReceive runs the system holds a lock (because of the Alarm manager) that will not fail. But between the context.startActivity(i); and the starting of the activity the system falls asleep again. You need to either use a WakefulBroadcastReceiver (see BroadcastReceiver Vs WakefulBroadcastReceiver) or (that's what I use) a WakefulIntentService.

In any case you need to start a service and start your activity from there. For the WakefulIntentService pattern see my answer PowerManager.PARTIAL_WAKE_LOCK android and links there.

Community
  • 1
  • 1
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361