2012-10-05 40 views
1

我想在特定事件发生时打开一个屏幕,而不管我现在在哪个屏幕上,我该怎么做?Android:从任何屏幕开始意向

Intent Iatualizar = new Intent(this, Atualizar.class); 
startActivity(Iatualizar); 

上面的代码工程,如果我与屏幕打开程序,但没有工作,当屏幕在后台。如何使它工作?谢谢

回答

1

FLAG_ACTIVITY_NEW_TASK添加到您的意图。如果你检查日志猫,它也会告诉你这一点。

+0

我是初学者,你能举个例子吗?谢谢! –

+0

Add:'Iactualizer.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);' – xandy

+0

即使添加我提供的代码仍然无效,有什么想法? –

0

试试这个,你也可以使用下面

Intent Iatualizar = new Intent(this, Atualizar.class); 
Iatualizar.addFlags(FLAG_ACTIVITY_NEW_TASK); 
startActivity(Iatualizar); 
1

的代码你应该只创建加载你的活动时,该事件发生时一个服务启动服务。

为此,您可以在加载应用程序时启动该服务,并将您的事件接收器放入服务类中。这样,即使其他应用程序正在运行且您的应用程序不在前台(显示在屏幕上),您仍然可以触发该事件。

如果Android垃圾收集器不在前台并且开始资源不足,Android垃圾收集器将尝试关闭您的活动。

如果您想更进一步,使服务成为前台服务,那么理论上它将是Android在内存不足时最终杀死的最后一件事。让我知道你是否需要一个代码示例。

希望这可以帮助你! 干杯

- 编辑 -

下面是一个代码示例,让您开始。

在您的活动onCreate调用一些类似于您上面启动您的服务的代码。 即:

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
Intent IatualizarService = new Intent(this, AtualizarService.class); 
startService(Iatualizar); 
} 

如果你愿意,也可以有这样的在你的onResume,(或从一个按钮来启动它,或者无论你想启动此服务)。

然后创建一个服务类,像这样:

public class AtualizarService extends Service { 
private final IBinder mBinder = new MyBinder(); 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 


    return START_STICKY; 
    } 

    @Override 
    public void onCreate() { 

// In here you can make your service a foreground service to help prevent garbage collection from occurring. 
makeforeground(); 

} 

    private boolean makeforeground() { 
     String msg = "Turning on foreground service"; 
     ErrorLog.i(getApplicationContext(), TAG, msg); 
     try { 
      Notification notification = new Notification(
        R.drawable.ic_dialog_info, 
        getText(R.string.notification_text), 
        System.currentTimeMillis()); 
      notification.flags |= Notification.FLAG_AUTO_CANCEL; 

      Intent activityIntent = new Intent(this, YourMainActivity.class); 
      activityIntent.setAction(Intent.ACTION_MAIN); 
      activityIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
      activityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
        | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, 
        activityIntent, 0); 
      notification.setLatestEventInfo(this, 
        getText(R.string.notification_title), 
        getText(R.string.notification_text), pendingIntent); 
      startForeground(1234567890, notification); // random id 
      return true; 
     } catch (Exception e) { 
      String error = "displayNotification Error Message: " 
        + e.getMessage() + " Cause: " + e.getCause(); 
      ErrorLog.e(GlobalParameters.getContext(), 
        TAG + " Notification Foreground Service", error); 
      return false; 
     } 
    } 

    @Override 
    public void onDestroy() { 
} 

    @Override 
    public IBinder onBind(Intent arg0) { 
    return mBinder; 
    } 
    public class MyBinder extends Binder { 
     AtualizarService getService() { 
      return AtualizarService .this; 
     } 
    } 
} 

然后,此服务类中,您可以在可以在任何你想要的事件触发广播接收器添加。然后加载你的活动,如果你想要。

干杯

+0

是的,我需要一个代码示例,请。 –

+0

好的,在代码示例中添加。 =) – Dave

+0

这对我来说太复杂了,你会有一个简单的方法来执行呼叫而不必创建服务?感谢您的时间和耐心。 –