How to solve error “Default Activity Not Found” occurred when starting my application from Service?

风流意气都作罢 提交于 2019-12-01 12:02:06

问题


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

This is my Service class.

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

This is my Activity class.

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

}

this is my AndroidManifest file.

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

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


回答1:


Put this in your Activity in manifest

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

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



回答2:


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

to

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



回答3:


Please refer below link. I hope this helps

https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&ved=0ahUKEwjsytHAhLLXAhWDuo8KHa4kAjoQFgg2MAM&url=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F15825081%2Ferror-default-activity-not-found&usg=AOvVaw0HThKZRxKk9ET-NdnVwEfR



来源:https://stackoverflow.com/questions/47208216/how-to-solve-error-default-activity-not-found-occurred-when-starting-my-applic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!