0

我想在我的应用程序中设置一个时间表。如果我的应用程序被应用程序任务杀手停止,我的应用程序会在它被杀死几秒钟后自动运行。

我的应用程序是这样的:

public class AlarmServiceDemo extends Activity 
{
    /** Called when the activity is first created. */
    private PendingIntent pendingIntent;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button buttonStart = (Button)findViewById(R.id.startalarm);
        Button buttonCancel = (Button)findViewById(R.id.cancelalarm);

        buttonStart.setOnClickListener(new Button.OnClickListener()
        {

            // @Override
            public void onClick(View arg0) {
            // TODO Auto-generated method stub

            Intent myIntent = new Intent(AlarmServiceDemo.this, MyAlarmService.class);
            pendingIntent = PendingIntent.getService(AlarmServiceDemo.this, 0, myIntent, 0);

            AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.add(Calendar.SECOND, 10);
            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

            Toast.makeText(AlarmServiceDemo.this, "Start Alarm", Toast.LENGTH_LONG).show();
            }
        });

        buttonCancel.setOnClickListener(new Button.OnClickListener()
        {

            //@Override
            public void onClick(View arg0) 
            {
                // TODO Auto-generated method stub
                AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
                alarmManager.cancel(pendingIntent);

                // Tell the user about what we did.
                Toast.makeText(AlarmServiceDemo.this, "Cancel!", Toast.LENGTH_LONG).show();
            }
        });
    }
}

这是我的服务:

public class MyAlarmService extends Service
{
    @Override
    public void onCreate() 
    {
        // TODO Auto-generated method stub
        Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
    }

    @Override
    public IBinder onBind(Intent intent) 
    {
        // TODO Auto-generated method stub
        Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
        return null;
    }

    @Override
    public void onDestroy() 
    {
        // TODO Auto-generated method stub
        super.onDestroy();
        Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onStart(Intent intent, int startId) 
    {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
        Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean onUnbind(Intent intent) 
    {
        // TODO Auto-generated method stub
        Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show();
        return super.onUnbind(intent);
    }
}

但是我的日程安排不行。我该如何解决?

4

1 回答 1

1

你必须通过启动应用程序准确地写出你的意思。如果你想启动一个活动,那么你可以写

@Override
public void onStart(Intent intent, int startId) {
 // TODO Auto-generated method stub
Intent activityIntent = new Intent(context,
                AlarmServiceDemo.class);
        activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(activityIntent);

 super.onStart(intent, startId);

}
于 2012-05-15T04:47:04.100 回答