【问题标题】:How to solve error "Default Activity Not Found" occurred when starting my application from Service?如何解决从服务启动我的应用程序时出现的错误“未找到默认活动”?
【发布时间】:2017-11-09 17:36:04
【问题描述】:

我需要使用 Service 启动我的 Android 应用程序,然后从该 Service 启动 Activity。首先,我通过引用 Stack Overflow 创建了 Sample Application。我创建了 StartReceiver 类来启动 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");
    }
}

这是我的服务类。

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

这是我的Activity课程。

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 给出 Error running app: Default Activity not found我该如何解决这个错误?

【问题讨论】:

  • 尝试文件 -> 使缓存无效/重新启动
  • 我试过了但是没有解决这个错误

标签: android android-activity android-service


【解决方案1】:

将其放入清单中的 Activity 中

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

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

【讨论】:

  • 我需要从服务而不是活动启动我的应用程序。
  • 尝试使用“this”而不是“getBaseContext()”来使用服务上下文。这是我能看到的唯一区别。
【解决方案2】:

android.intent.action.MAIN表示这个activity是应用程序的入口点,即当你启动应用程序时,这个activity就被创建了。

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

所以您需要在清单中添加它。只需更改:

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

【讨论】:

  • 我需要从服务启动我的应用程序。并且在 MainActivity 需要从该 Service 启动之后。
【解决方案3】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-28
  • 2019-06-19
  • 1970-01-01
  • 2020-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多