开发者

Alarm In Android

开发者 https://www.devze.com 2023-01-15 17:07 出处:网络
public class Alarm extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) {
public class Alarm extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent intent = new Intent(this, AlarmReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
        intent, PendingIntent.FLAG_ONE_SHOT);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000), pendingIntent);
        Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
    }开发者_Go百科
}

and

public final class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
    }
}

After 10 secs am gettign toast as alarm worke..but no default sound ?how get the sound for alarm in android and instead of toas how can i display an dialog..?


AlarmManager has nothing to do with sounds. You are perhaps confusing AlarmManager with an alarm clock application. You are welcome to play a sound via your BroadcastReceiver, though I have not tried this.


NotificationManager nm;
@Override
public void onReceive(Context context, Intent intent) {
    nm = (NotificationManager) context.getSystemService(
        Context.NOTIFICATION_SERVICE);
    CharSequence from = "Check your..";
    CharSequence message = "It's time !";
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
        new Intent(), 0);
    Notification notif = new Notification(R.drawable.ic_launcher,
        "ut text", System.currentTimeMillis());
    notif.setLatestEventInfo(context, from, message, contentIntent);
    notif.defaults |= Notification.DEFAULT_SOUND; 
    notif.flags |= Notification.FLAG_AUTO_CANCEL; 
    nm.notify(1, notif);
0

精彩评论

暂无评论...
验证码 换一张
取 消