[AlarmController.java]

private OnClickListener mOneShotListener = new OnClickListener() {
        public void onClick(View v) {

            Intent intent = new Intent(AlarmController.this, OneShotAlarm.class);
            PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
                    0, intent, 0);

            Calendar calendar = Calendar.getInstance();
          calendar.setTimeInMillis(System.currentTimeMillis());
          calendar.add(Calendar.SECOND, 30);

          AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);

            if (mToast != null) {
                mToast.cancel();
            }
    mToast = Toast.makeText(AlarmController.this,R.string.one_shot_scheduled,
            Toast.LENGTH_LONG);
            mToast.show();

        }
    };

private OnClickListener mStartRepeatingListener = new OnClickListener() {
        public void onClick(View v) {
       
            Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class);
            PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,
                    0, intent, 0);
           
            long firstTime = SystemClock.elapsedRealtime();
            firstTime += 15*1000;

      AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                            firstTime, 15*1000, sender);.

            if (mToast != null) {
                mToast.cancel();
            }
            mToast = Toast.makeText(AlarmController.this,
                      R.string.repeating_scheduled, Toast.LENGTH_LONG);
            mToast.show();
        }
    };

    private OnClickListener mStopRepeatingListener = new OnClickListener() {
        public void onClick(View v) {
         
            Intent intent = new Intent(AlarmController.this, RepeatingAlarm.class);
            PendingIntent sender = PendingIntent.getBroadcast
                                           (AlarmController.this, 0, intent, 0);
           
     AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            am.cancel(sender);

            if (mToast != null) {
                mToast.cancel();
            }
        mToast = Toast.makeText(AlarmController.this,
                R.string.repeating_unscheduled, Toast.LENGTH_LONG);
            mToast.show();
        }
    };
}



[OneShotAlarm.java]

public class OneShotAlarm extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Toast.makeText(context, R.string.one_shot_received,
                                  Toast.LENGTH_SHORT).show();
    }
}


[RepeatingAlarm.java]

public class RepeatingAlarm extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Toast.makeText(context, R.string.repeating_received,
                                         Toast.LENGTH_SHORT).show();
    }
}



[Manifest.xml]

<receiver android:name=".app.OneShotAlarm" android:process=":remote" />

<receiver android:name=".app.RepeatingAlarm" android:process=":remote" />

<activity android:name=".app.AlarmController"
                                    adroid:label="@string/activity_alarm_controller">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.SAMPLE_CODE" />
            </intent-filter>
        </activity>

<service android:name=".app.AlarmService_Service"
                                                            android:process=":remote" />


 

'old > API_Demo' 카테고리의 다른 글

APP_Dialog_AlertDialogSamples  (0) 2010.03.22
APP_Alarm_AlarmService  (0) 2010.03.22
APP_Activity_Wallpaper  (0) 2010.03.16
APP_Activity_TranslucentBlurActivity  (0) 2010.03.16
APP_Activity_TranslucentActivity  (0) 2010.03.16
Posted by jazzlife
,