设备锁定时显示AlertDialog

时间:2012-03-13 20:58:51

标签: java android

我必须开发一个应用程序,在2小时内每10分钟显示一次AlertDialog [Alarm] [从用户点击StartButton的时间开始],实际上我键入了这段代码,它显示了alertDialog,但是当我在手机上测试它时移动锁定时不会出现AlertDialog,但是,我仍然有一些问题,

这是我的代码: -

//Activity

public class AndroidAlarmService extends Activity {

private PendingIntent pendingIntent;


 /** Called when the activity is first created. */
 @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 View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
         Intent myIntent = new Intent(AndroidAlarmService.this, BroadCastReceiverFor.class);
             pendingIntent = PendingIntent.getBroadcast(AndroidAlarmService.this, 234324243, myIntent, 0);

             Calendar calendar = Calendar.getInstance();
             calendar.setTimeInMillis(System.currentTimeMillis());
             calendar.add(Calendar.MINUTE, 10);       

             AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
             alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);



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

     buttonCancel.setOnClickListener(new View.OnClickListener() {

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

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

 }

}
//BroadcastReceiver


public class BroadCastReceiverFor extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Intent service = new Intent(context, MyAlarmService.class);
        context.startService(service);

    }
}

// Service

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(); 
            Intent dialog = new Intent(this, ShowingDialog.class);
            dialog.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
            startActivity(dialog);  

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



} 


// Another Activity to show the AlertDialog


public class ShowingDialog extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        //For Notification

    final AlertDialog alertDialog = new AlertDialog.Builder(this).create();

    //For Notification
            alertDialog.setTitle("Conformation");
            alertDialog.setMessage("Are you sure you want to Do that?");


                    alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int which) {


                       }
                    });



                    alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
                    alertDialog.show();

    }




}

// Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.exercise.AndroidAlarmService"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="4" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AndroidAlarmService"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


    <activity 
    android:name=".ShowingDialog"
     android:theme="@android:style/Theme.Dialog"/> 


        <service android:name=".MyAlarmService" />
        <receiver android:name="BroadCastReceiverFor"></receiver>

    </application>
</manifest>

我的问题是:

1-即使设备被锁定,如何显示此alertDialog? 2-如何在20分钟后再次显示对话框?

感谢adavance ..

1 个答案:

答案 0 :(得分:0)

要让您的活动即使在设备处于睡眠状态时启动,您也需要acquire a WakeLock

This page has a nice overview of how it is done.

要再次触发警报,只需覆盖AlertDialog上的按钮即可创建新的PendingIntent,并使用AlarmManager以与第一次相同的方式设置它。