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 ?
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>
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>
来源:https://stackoverflow.com/questions/47208216/how-to-solve-error-default-activity-not-found-occurred-when-starting-my-applic