如何解决错误“未找到默认活动";从服务启动我的应用程序时发生错误?

如何解决错误“未找到默认活动

问题描述:

我需要使用服务启动我的 Android应用,然后从该服务启动活动.首先,我通过引用堆栈溢出来创建示例应用程序.我创建了 StartReceiver 类来启动 Service .

I need to start my Android Application using Service and after start an Activity from that Service. Firstly I created Sample Application by referring Stack Overflow. I created StartReceiver class to start Service.

public class StartReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent arg1)
    {
        Intent intent = new Intent(context,MyService.class);
        context.startService(intent);
        Log.i("Autostart", "started");
    }
}

这是我的 Service 类.

public class MyService extends Service
{
    private static final String TAG = "MyService";
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    public void onDestroy() {
        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy");
    }

    @Override
    public void onStart(Intent intent, int startid)
    {
        Intent intents = new Intent(getBaseContext(),MainActivity.class);
        intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intents);
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onStart");
    }
}

这是我的活动课程.

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast.makeText(getBaseContext(), "Hello........", Toast.LENGTH_LONG).show();
    }

}

这是我的 AndroidManifest 文件.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.onbitlabs.runwithoutactivity" android:versionCode="1" android:versionName="1.0"

android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

<application android:icon="@mipmap/ic_launcher" android:label="@string/app_name">

    <receiver android:name=".StartReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

    <activity android:name=".MainActivity"></activity>
    <service android:enabled="true" android:name=".MyService" />
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
</manifest>

当我运行应用程序时,Android Studio会给出正在运行的应用程序出错:未找到默认活动. 如何解决此错误?

When I Run the Application, Android Studio gives Error running app: Default Activity not found. How could I Fix this Error ?

android.intent.action.MAIN表示此活动是应用程序的入口点,即,当您启动应用程序时,将创建此活动.

android.intent.action.MAIN means that this activity is the entry point of the application, i.e. when you launch the application, this activity is created.

android.intent.category.LAUNCHER表示应在应用程序启动器中列出入口点

android.intent.category.LAUNCHER says that entry point should be listed in the application launcher

因此,您需要在清单中添加它.只需更改:

So you need to add that in your manifest. Just change:

<activity android:name=".MainActivity"></activity>

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