[英]change activity after a certain time period
I am creating app that changes activities after time period, but I can`t make it work. 我正在创建可以在一段时间后更改活动的应用程序,但是我无法使其正常运行。 When I run the program I get only black screen.
当我运行程序时,我只会看到黑屏。 Here is my onCreate method:
这是我的onCreate方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
activity = 1;
i = new Intent(context, WeatherActivity.class);
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
while (true){
if(activity ==1){
context.startActivity(i);
activity = 0;
}else if (activity ==0){
news();
activity = 1;
} try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}, 10000L);
}
Activities are just simple activies with just some text. 活动只是带有一些文本的简单活动。 I`ll post them if needed.
如有需要,我会发布它们。 Edit: Made it work by changin activities in methods.
编辑:通过改变方法的活动使其起作用。
You are missing setContentView
您缺少
setContentView
super.onCreate(savedInstanceState);
setContentView(R.layout.your_xml_name);
Check 校验
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_xml_name);
context = this;
activity = 1;
Check Splash screen 检查启动画面
To get things done in tandem you can use AlarmManager. 要同时完成任务,可以使用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
Then extends BroadcastReceiver add to your activity . 然后将BroadcastReceiver扩展添加到您的活动中。
public class YourActivity extends BroadcastReceiver { 公共类YourActivity扩展了BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// your task
}
} }
RegisterReceiver your BroadcastReceiver @Override protected void onResume() { super.onResume(); RegisterReceiver您的BroadcastReceiver @Override受保护的void onResume(){super.onResume(); mIntentFilter=new IntentFilter("com.foo.android.MY_TIMER");
mIntentFilter = new IntentFilter(“ com.foo.android.MY_TIMER”); registerReceiver(mReciever, mIntentFilter);
registerReceiver(mReciever,mIntentFilter); }
}
Remember to run this in the onDestroy method: 记住要在onDestroy方法中运行它:
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
Well, I suppose your problem is that you are executing an infinite loop in the main thread, it will never have the opportunity to draw the new activity layout. 好吧,我想您的问题是您正在主线程中执行无限循环,它将永远不会有机会绘制新的活动布局。
A possible solution is to use AsyncTask
described here . 一个可能的解决方案是使用此处描述的
AsyncTask
。
Essentially, it allows you to perform long running task in background and to publish results on the main thread when they are ready. 本质上,它允许您在后台执行长时间运行的任务,并在准备好结果后将结果发布到主线程上。
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);
}
}
Now you just have to instantiate and execute it where you want: 现在,您只需实例化并在所需的位置执行它:
new MyAyncTask(context).execute(100000);
You can do also more complicated things with AsyncTask
, like publishing intermediate results with publishProgress
and onProgressUpdate
(see the documentation for more information), this was just a basic example. 您还可以使用
AsyncTask
做更复杂的事情,例如使用publishProgress
和onProgressUpdate
发布中间结果(有关更多信息,请参见文档),这只是一个基本示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.