我想显示警报对话框,但它目前正处于应用程序界面。这是一种方法吗?
Intent i = new Intent("com.example.servicealarmdemo2.demoactivity");
PendingIntent operation = PendingIntent.getActivity(
getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);
我是否必须更改PendingIntent变量?
答案:
我终于找到了最简单的方法。刚刚在MainActivity类中添加了命令“finish()”以完成活动并返回主屏幕,然后可以在那里显示警报。
希望它会帮助其他陷入此问题的人
答案 0 :(得分:0)
如果您想在主屏幕中创建应用,请参阅以下链接
但是,如果您希望自己的代码正常运行,即使您的手机也使用KeyguardLock和WakeLock。以下是我的工作:
public class DismissLock extends Activity {
PowerManager pm;
WakeLock wl;
KeyguardManager km;
KeyguardLock kl;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.i("INFO", "onCreate() in DismissLock");
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
km=(KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
kl=km.newKeyguardLock("INFO");
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.ON_AFTER_RELEASE, "INFO");
wl.acquire(); //wake up the screen
kl.disableKeyguard();// dismiss the keyguard
setContentView(R.layout.main);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
wl.release(); //when the activiy pauses, we should realse the wakelock
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
wl.acquire();//must call this!
}
}
当然,您仍需要在清单文件中声明权限。
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
//// EDIT
如果您想加入两项工作,您必须使用以下代码编辑代码:
boolean fDialogMode = getIntent().hasExtra( "dialog_mode" );
if( ! fDialogMode ) {
super.setTheme( android.R.style.Theme_Dialog );
}
AlertDemo alert = new AlertDemo();
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
km=(KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
kl=km.newKeyguardLock("INFO");
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.ON_AFTER_RELEASE, "INFO");
wl.acquire(); //wake up the screen
kl.disableKeyguard();// dismiss the keyguard
/** Opening the Alert Dialog Window */
alert.show(getSupportFragmentManager(), "AlertDemo");
并在另一个包或接收器中添加以下代码:
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("com.example.openlock");
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
答案 1 :(得分:0)
使用服务
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.getApplicationContext().startActivity(intent);
下面是一些代码
public class HomepopupDataService extends Service {
private static final String TAG = "HomepopupDataService";
@Override
public void onCreate() {
Log.i(TAG, "Service onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.i(TAG, "Service onStartCommand");
CountDownTimer dlgCountDown;
Log.e("---------------", "onHandleIntent");
dlgCountDown = new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
Log.e("---------------", "onHandleIntent++");
}
public void onFinish() {
Intent i = new Intent(getApplicationContext(),
DialogActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(i);
}
}.start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
Log.i(TAG, "Service onBind");
return null;
}
@Override
public void onDestroy() {
Log.i(TAG, "Service onDestroy");
}
}