为什么我的应用需要两次打开?

时间:2012-02-07 09:07:37

标签: android crash

我的应用程序出于某种原因在第一次打开时失败,但是第二次点击它就打开了。这种情况有什么共同的原因吗?

以下是主要活动的代码,我们将非常感谢任何信息:

package com.chich;

import android.app.Activity;

import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class chich_activity extends Activity 
{

Button btnSendSMS;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);  

    ComponentName locationReceiver = new ComponentName(this, SmsReceiver.class);
    PackageManager pm = getPackageManager();
    pm.setComponentEnabledSetting(locationReceiver,
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);



    btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
    btnSendSMS.setOnClickListener(new OnClickListener() 
    {


        public void onClick(View v) 
        {     
            Intent i = new Intent(chich_activity.this, chich_activity2.class);
            startActivity(i);

    }    
});
    }
}

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.chich"
  android:versionCode="1"
  android:versionName="1.0.0">
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
    <activity android:name=".chich_activity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>     

       <activity android:name=".chich_activity2"
              android:label="@string/app_name">
        <intent-filter>
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity> 

    <receiver android:name=".SmsReceiver"> 
        <intent-filter android:priority="9999999" >
            <action android:name = "android.provider.Telephony.SMS_RECEIVED" /> 
        </intent-filter> 
    </receiver>

</application>
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
</manifest>

3 个答案:

答案 0 :(得分:1)

答案来自包管理器。事实证明,如果你不在最后编写PackageManager.DONT_KILL_APP,它将关闭应用程序,如下所示:

ComponentName locationReceiver = new ComponentName(this, SmsReceiver.class);
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(locationReceiver,
        PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

答案 1 :(得分:0)

尝试只将一个活动作为启动器。从此处删除此行<category android:name="android.intent.category.LAUNCHER" />

<activity android:name=".chich_activity2"
              android:label="@string/app_name">
        <intent-filter>
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity> 

答案 2 :(得分:0)

我有同样的问题,不是最好的方式,但对我有用!

 switch(estado) {
                case 0:
                    if (!tcursor.moveToFirst()){
                        estado = 1;

                            Intent intentConfig=new Intent(getApplicationContext(),Configuracion.class);
                            startActivity(intentConfig);
                    }
                    break;
                default:

            }