0
我最近開始研究一個簡單的Android項目,但看起來我無法使其工作。我有一個主要的活動(它有@ style/Invisible參數),它檢查我的服務是否正在運行,如果不是,則啓動它。另外,在檢查之前,它會啓動另一個結果活動。該活動預計會返回用戶的用戶名和密碼以允許應用程序登錄到系統。問題是,當我在我的手機上安裝應用程序時,它工作正常,但下次打開應用程序時什麼也沒有發生。我必須重新安裝它。單擊圖標時未啓動活動
這裏的主要活動:
@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活動:
@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();
}
這裏的清單:
<?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>
我在做什麼錯?
感謝您的幫助!
請分享xml,清單和服務代碼 –
您的onSendData方法在哪裏調用? – mmcoder10
@mman按下按鈕時調用它。在XML中,它具有'android:onClick =「onSendData」'參數。 –