为什么我的应用程序需要两次才能打开?

发布于 2025-01-03 03:45:01 字数 2754 浏览 1 评论 0原文

由于某种原因,我的应用程序第一次打开时失败,但第二次单击它时打开正常。发生这种情况有什么常见原因吗?

这是主要活动的代码,任何信息将不胜感激:

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>

My app for some reason fails the first time it opens, but then opens fine the second time I click on it. Are there any common reasons this happens?

Here is the code for the main activity, any information would be greatly appreciated:

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);

    }    
});
    }
}

Manifest file:

<?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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深府石板幽径 2025-01-10 03:45:01

答案来自包管理器。事实证明,如果您最后不写 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);

The answer came in the package manager. Turns out it shuts the application off if you don't write in the end, PackageManager.DONT_KILL_APP as in:

ComponentName locationReceiver = new ComponentName(this, SmsReceiver.class);
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(locationReceiver,
        PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
泛泛之交 2025-01-10 03:45:01

尝试仅保留一项活动作为启动器。从此处删除此行

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

Try to leave only one activity as launcher. Remove this row <category android:name="android.intent.category.LAUNCHER" /> from here

<activity android:name=".chich_activity2"
              android:label="@string/app_name">
        <intent-filter>
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity> 
愿得七秒忆 2025-01-10 03:45:01

我遇到了同样的问题,这不是最好的方法,但对我有用!

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

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

            }

I had same issue, is not the best way but work for me!

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

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

            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文