基本上,这就是我正在做的事情
1)设置AlarmManager以执行BroadcastReceiver(BCR)
Intent intent = new Intent(m_Context, BCR.class);
intent.putExtras(extras);
PendingIntent pendingIntent = PendingIntent.getBroadcast(m_Context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, StartTime, pendingIntent)
Run Code Online (Sandbox Code Playgroud)
2)从BCR启动MyActivity
@Override
public void onReceive(Context context, Intent intent) {
Intent newIntent = new Intent(context, MyActivity.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(newIntent);
}
Run Code Online (Sandbox Code Playgroud)
3)如果MyActivity未打开,请将其打开
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.myactivity);
}
@Overide
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,我注意到当MyActivity打开时,它的流程如下:
onCreate/onNewIntent - > onResume - > onPause - > onResume
我不确定为什么它会立即进行onPause.我注意到这只发生在标志打开被筛选时.有谁知道为什么会这样?有什么办法可以防止这种行为?
我正在编写一个在固定时间显示活动的应用程序.
我用这样的代码从服务开始活动:
intent.setClass(context, FakeAction.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
在FakeAction的onCreate方法中,我需要唤醒设备并启动声音消息:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
Run Code Online (Sandbox Code Playgroud)
onCreate中有很多代码可以在屏幕上显示信息并启动声音信息.
这是一个onPause方法:
@Override
protected void onPause()
{
Log.i(TAG, "onPause");
//Stop mediaPlayer
if(mp != null)
{
if(mp.isPlaying())mp.stop();
mp.release();
mp = null;
}
//Restore volume
if(audioManager != null)
{
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, savedVolume, 0);
}
//Stop minute timer
handler.removeCallbacks(minuteReceiver);
super.onPause();
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,在onCreate之后立即调用onPause()方法.所以我的声音消息立即停止.
但是如果在未锁定屏幕时启动活动,则在onCreate()之后不会立即调用onPasue();
即使我评论所有"getWindow().addFlags()"字符串,onCreate()在onCreate()之后调用,如果屏幕被锁定.
问题是为什么在onCreate之后立即调用onPause?当用户按下后退按钮时,如何区分onPause()方法的立即调用和onPause()的调用?
以下是活动的代码.我使用MVP模式,因此,主要代码在演示者中.但即使我评论所有演示者的调用(就像我在本例中所做的那样),onCreate()之后立即调用onPause()
我是否可以在AsyncTask中开始活动?AsyncTask是从服务启动的.如果完成,则在AsyncTask之后停止服务.
public class FakeAction extends RoboActivity
implements
View.OnClickListener
,AlarmAction
{
private static final String TAG = "TA FakeAction";
@InjectView(R.id.aa_btn_Stop) Button btnStop;
@InjectView(R.id.aa_btn_Snooze) Button btnSnooze;
@InjectView(R.id.aa_tv_CurTime) TextView tvCurTime; …Run Code Online (Sandbox Code Playgroud) 我在onPause()onResume()摄像机实时循环中遇到了一些麻烦:带预览的摄像机,拍照完全没问题.除了一个例外:
我启动应用程序,单击主页按钮,切换回应用程序并再次拍摄.
结果:仍然执行了shuttercallback(参见代码),但jpeg回调不再了!然后我的星系S振动,屏幕保持黑色,因为在jpegCallback之后没有重新触发startPreview().堆栈跟踪对我来说远非有用.奇怪的是,这只发生在我的Galaxy S上,而不是在模拟器上.我真的不知道如何继续前进:/任何人都知道什么是有用的?
10-28 18:59:40.649: ERROR/SecCamera(4291): SetRotate(angle(0)) 10-28 18:59:40.649: ERROR/CameraHardwareSec(4291): ====setParameters processingmethod = (null) 10-28 18:59:40.649: ERROR/SecCamera(4291): setRecordingSize(width(800), height(480)) 10-28 18:59:40.673: ERROR/SecCamera(4291): SetRotate(angle(0)) 10-28 18:59:40.673: ERROR/CameraHardwareSec(4291): ====setParameters processingmethod = (null) 10-28 18:59:40.673: ERROR/SecCamera(4291): setRecordingSize(width(800), height(480)) 10-28 18:59:40.692: ERROR/SecCamera(4291): SetRotate(angle(0)) 10-28 18:59:40.692: ERROR/CameraHardwareSec(4291): ====setParameters processingmethod = (null) 10-28 18:59:40.692: ERROR/SecCamera(4291): setRecordingSize(width(800), height(480)) 10-28 18:59:40.712: ERROR/SecCamera(4291): SetRotate(angle(0)) 10-28 18:59:40.712: ERROR/CameraHardwareSec(4291): ====setParameters processingmethod = (null) 10-28 18:59:40.712: ERROR/SecCamera(4291): setRecordingSize(width(800), height(480)) 10-28 18:59:40.751: ERROR/CameraHardwareSec(4291): stopPreview() 10-28 18:59:40.751: ERROR/SecCamera(4291): cancelAutofocus() 10-28 18:59:40.751: ERROR/SecCamera(4291): cancelAutofocus() …
出于某种原因,我正在处理的应用程序正在显示一个吐司,显示我的设备上留下的内部存储空间,即使我没有编写此代码.
这是一个截图
http://i.stack.imgur.com/z2ERU.png
这里是(仅)活动的代码.
http://pastie.org/8382286
正如你所看到的,所有的祝酒词都被注释掉了,我甚至都没有进口android.widget.Toast.然而,我仍然在屏幕截图中显示了祝酒词.
谁能告诉我为什么会这样?