简体   繁体   English

单击图标后活动未开始

[英]Activity not starting when the icon is clicked

I've began working on a simple Android project recently, but it looks like I just cannot get it to work. 我最近开始研究一个简单的Android项目,但看来我无法使其正常工作。 I have a main Activity (It has @style/Invisible parameter) which checks if my service is running and starts it if it's not. 我有一个主要的Activity(它具有@ style / Invisible参数),它检查我的服务是否正在运行,如果没有运行,则将其启动。 Also, before the check takes place it starts another activity for result. 同样,在进行检查之前,它会开始另一个活动以求结果。 The activity is expected to return the user's username and password to allow the app to login to the system. 预期该活动将返回用户的用户名和密码,以允许该应用程序登录到系统。 The problem is that when I install the app on my phone it works fine, but next time I open the app nothing happens. 问题是,当我在手机上安装该应用程序时,它可以正常工作,但是下次我打开该应用程序时,什么也没发生。 I have to reinstall it. 我必须重新安装它。

Here's the main Activity: 这是主要活动:

   @Override
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

    Intent intent = new Intent(this, LoginGrabber.class);
    startActivityForResult(intent, 100);

    if(isMyServiceRunning()==false) 
    {       
        startService(new Intent(getApplicationContext(), MyService.class));
        Log.i("com.connect","startService");
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if(requestCode == 100){
        if(resultCode == RESULT_OK){
            String username = data.getStringExtra("Username");
            String password = data.getStringExtra("Password");
            //TODO Send to server
            Toast.makeText(this, "Username: " + username + "     Password: " + password, Toast.LENGTH_LONG);
        }
    }
}

private boolean isMyServiceRunning() {
    ActivityManager manager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (MyService.class.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

LoginGrabber Activity: LoginGrabber活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_grabber);
}

public void onSendData(View view){
    Intent intent = new Intent();
    intent.putExtra("Username", ((TextView) findViewById(R.id.email)).getText());
    intent.putExtra("Password", ((TextView) findViewById(R.id.password)).getText());
    setResult(RESULT_OK, intent);
    finish();
}

Here's the Manifest: 这是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.test"
android:versionCode="2"
android:versionName="2.0">

<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" />

<supports-screens
android:largeScreens="true"
android:resizeable="true"
android:xlargeScreens="true" />

<uses-permission
android:name="android.permission.INTERNET"
android:required="true" />

<application
android:icon="@drawable/launcher"
android:label="@string/app_name"
android:theme="@style/Invisible">
<activity
android:name="com.connect.Main"
android:excludeFromRecents="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity

<service
android:name="com.connect.MyService"
android:enabled="true"
android:exported="true" />

<activity
android:theme="@style/AppTheme"
android:name="com.connect.LoginGrabber"
android:label="@string/title_activity_login_grabber" />
</application>

</manifest>

What am I doing wrong? 我究竟做错了什么?

Thanks for your help! 谢谢你的帮助!

OK! 好! I found the problem! 我发现了问题! The problem was that my application was crashing as soon as the service was started because I was testing the app without internet and without handling errors that were thrown when there was no internet available. 问题是服务启动后我的应用程序崩溃了,因为我在没有Internet的情况下测试了该应用程序,并且没有处理没有Internet时抛出的错误。

Just had to turn WiFi on, that's it! 只需打开WiFi,就是这样!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM