java - 如何创建一个在设备打开时设置为默认值的 Android 应用程序

标签 java android android-launcher

我想在 android 中创建一个应用程序,安装后我需要在设备打开时将其设置为默认值。

我尝试过以下方法

创建了一个服务, 创建一个接收器来接收启动完成事件

它实际上不起作用。如果我删除 MainActivity 的启动器,它将显示错误

No Launcher activity found!
The launch will only sync the application package on the device!

我的 list 文件

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

         <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="sensorLandscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".MyService"
            android:label="My Service" >
            <intent-filter>
                <action android:name="com.example.splash_test.MyService" />
            </intent-filter>
        </service>

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


            </intent-filter>
        </receiver>

         <activity
            android:name="com.example.splash_test.Screen1"
            android:label="@string/app_name"
            android:screenOrientation="sensorLandscape" >
            </activity>
    </application>

MyReceiver.java

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Intent serviceIntent = new Intent(context, MyService.class);
            context.startService(serviceIntent);
        }
    }

}

MyService.java

public class MyService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub

        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {

            Intent i = new Intent(this, MainActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            this.startActivity(i);
        }
        return null;
    }

}

最佳答案

您必须按照以下步骤来完成此任务:-

  1. 在您的应用中创建一个服务(始终运行)。
  2. 检查您的应用是否正在运行。(通过包名称)
  3. 如果正在运行,那么在服务的帮助下运行则很好。

尝试一下......它在我的应用程序中运行良好。 :)

关于java - 如何创建一个在设备打开时设置为默认值的 Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34505004/

相关文章:

java - TouchWiz 启动器检测

java - 将 Jar 包含在其他提供错误中

android - 如何从应用程序更改启动器图标及其标签

java - 无法转换 opennlp Parse 的结果

java - 如何通知 MediaStore 某些项目已删除?

android - Retrofit2 和 RxJava2 : How to detect the network error

android - 错误安装 Android 目标 - Cordova CLI

android - 您的应用程序自动进入主页的快捷方式

Java 动态代理 - 如何引用具体类

java - 如何控制 MIDI 序列的音量