我正在创建在时间段之后更改活动的应用程序,但我无法使其正常工作。当我运行程序时,我只得到黑屏。这是我的onCreate方法:
ldd
活动只是简单的活动,只有一些文字。如果需要,我会发布它们。 编辑:通过方法中的changin活动使其工作。
答案 0 :(得分:1)
您缺少super.onCreate(savedInstanceState);
setContentView(R.layout.your_xml_name);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_xml_name);
context = this;
activity = 1;
检查强>
{{1}}
答案 1 :(得分:0)
要完成任务,您可以使用AlarmManager。
Intent intent = new Intent("com.foo.android.MY_TIMER");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
long now = System.currentTimeMillis();
long interval = 60 * 60 * 1000; // 1 hour
manager.setRepeating(AlarmManager.RTC_WAKEUP, now + interval, interval,
pendingIntent); // Schedule timer for one hour from now and ev
然后将BroadcastReceiver添加到您的活动中。
公共类YourActivity扩展了BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// your task
}
}
RegisterReceiver你的BroadcastReceiver @覆盖 protected void onResume(){ super.onResume(); mIntentFilter = new IntentFilter(“com.foo.android.MY_TIMER”); registerReceiver(mReciever,mIntentFilter); }
请记住在onDestroy方法中运行它:
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
答案 2 :(得分:0)
好吧,我想你的问题是你在主线程中执行一个无限循环,它永远不会有机会绘制新的活动布局。
可能的解决方案是使用AsyncTask
描述的class MyAsyncTask extends AsyncTask<Integer, Void, Void> {
private Context context;
public MyAsyncTask(Context c) { context = c; }
public void doInBackground(Integer... milliseconds) {
try {
for(int i = 0; i < milliseconds.length; ++i)
Thread.sleep(milliseconds[i]);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void onPostExecute(Void result) {
Intent intent = new Intent(context, WeatherActivity.class);
context.startActivity(intent);
}
}
。
基本上,它允许您在后台执行长时间运行的任务,并在准备就绪时在主线程上发布结果。
new MyAyncTask(context).execute(100000);
现在你只需要在你想要的地方实例化并执行它:
AsyncTask
您可以使用publishProgress
执行更复杂的操作,例如使用onProgressUpdate
和html{
height:100%;
min-height:100%;
}
body{height:100%;}
header{height:10%;
background:red;
width:100%;
margin:0 auto;}
发布中间结果(有关详细信息,请参阅文档),这只是一个基本示例。< / p>