【问题标题】:How to call another activity after certain time如何在一定时间后调用另一个活动
【发布时间】:2013-05-01 07:11:40
【问题描述】:

此时我尝试创建一个 Android TV 应用程序。

例如,我有 3 个活动。我想在 10 秒后调用另一个活动。如何使用Android C# 创建它?

Activity 1
---------- (after 10 sec) ->
Activity 2
---------- (after 10 sec) ->
Activity 3
---------- (after 10 sec) -> AGAIN to Activity 1

解决这个问题的最佳方法是什么?

【问题讨论】:

  • 你有在 Android C# 中使用 Timer 的例子吗?
  • 使用处理程序可以做到这一点,但在核心 android 中。

标签: c# android xamarin


【解决方案1】:

我已经找到了解决方案。你必须使用线程。

new Thread(new ThreadStart(() =>
                                 {
        Thread.Sleep(10000);
        RunOnUiThread(() =>
                      {
          Intent i = new Intent();
          i.SetClass(this, typeof(Activity2));
          StartActivity(i);

          this.Finish();
        });


      })).Start();   

【讨论】:

    【解决方案2】:

    您可以使用 AlaramManager 设置调用活动的时间。

    Intent intentA=new Intent(context, A.class);
    
    pendingIntent=PendingIntent.getBroadcast(this, 0, intentA, 0);
    
    alarmManager=(AlarmManager)getSystemService(ALARM_SERVICE);
    
    alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,0,3000, pendingIntent);
    
    
    Intent intentB=new Intent(context, B.class);
    
    pendingIntent=PendingIntent.getBroadcast(this, 0, intentB, 0);
    
    alarmManager=(AlarmManager)getSystemService(ALARM_SERVICE);
    
    alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,1000,3000, pendingIntent);
    
    
    Intent intentC=new Intent(context, C.class);
    
    pendingIntent=PendingIntent.getBroadcast(this, 0, intentC, 0);
    
    alarmManager=(AlarmManager)getSystemService(ALARM_SERVICE);
    
    alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,2000,3000, pendingIntent);
    

    我没有尝试过,但我认为只需稍加修改即可。

    【讨论】:

    • 不幸的是,我正在寻找 C# 解决方案。 :(
    【解决方案3】:

    不使用任何处理程序来实现这一点。这里这个活动在 2 秒后开始

         Handler handler = new Handler();
                   Runnable r=new Runnable() {
                             @Override
                             public void run() {
                               startActivity(new intent(xxx.this,yyy.class);
                             }         
                           };
                       handler.postDelayed(r, 2000);
    

    【讨论】:

    • 不幸的是,我正在寻找 C# 解决方案。 :(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    相关资源
    最近更新 更多