【问题标题】:Android: Start Activity A if service running else start activity BAndroid:如果服务正在运行,则启动活动 A,否则启动活动 B
【发布时间】:2014-12-18 21:36:03
【问题描述】:

我遇到了这个问题,无法让它工作。这是空的主类别启动器活动的代码,如果服务未运行或用户未通过身份验证,则必须显示启动屏幕,否则启动对话活动。

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;

public class EntryPoint extends Activity {
    private IAppManager imService;

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {

            imService = ((IMService.IMBinder) service).getService();

            // this is not starting activity :(

            // Start converstion activity if service running and user ok
            if (imService.isUserAuthenticated() == true) {
                try {
                    Intent i = new Intent(EntryPoint.this, Conversations.class);
                    startActivity(i);
                    finish();

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }

        // this is not working

        // start login activity if service disconnected

        public void onServiceDisconnected(ComponentName className) {

            imService = null;

            try {
                Intent intent = new Intent(getBaseContext(), Splash.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                startActivity(intent);
                finish();

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Start and bind the imService
        startService(new Intent(EntryPoint.this, IMService.class));
    }

    @Override
    protected void onPause() {

        super.onPause();

        try {
            unbindService(mConnection);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {

        super.onResume();

        try {

            bindService(new Intent(EntryPoint.this, IMService.class),
                    mConnection, Context.BIND_AUTO_CREATE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

当我运行应用程序时,ConversationsSplash 活动都没有运行,而是我看到空活动:( 也没有错误,只是运行空的EntryPoint 活动,实际上应该启动其他活动之一活动。

有谁知道我在这里做错了什么?

【问题讨论】:

    标签: android android-activity


    【解决方案1】:

    很可能您的服务未连接,因此无法与您的活动交互。

    单独创建一个intent,然后在startService & bindService处赋值,而不是每次都为startService & bindService创建一个新的intent实例。

    另外你为什么在 onResume() 绑定服务?查看此链接,了解为什么您应该尽可能避免这种情况 - Binding to Service in onCreate() or in onResume()

    Intent serviceIntent = new Intent(EntryPoint.this, IMService.class);
    startService(serviceIntent);
    bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE);
    

    【讨论】:

    • 您好,感谢您的回答。我试过了,但它仍然显示空白活动,尽管没有错误。飞溅和对话活动都没有开始。我不知道如何等待服务连接然后做事:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多