我想在 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;
}
}
最佳答案
您必须按照以下步骤来完成此任务:-
- 在您的应用中创建一个服务(始终运行)。
- 检查您的应用是否正在运行。(通过包名称)
- 如果正在运行,那么在服务的帮助下运行则很好。
尝试一下......它在我的应用程序中运行良好。 :)
关于java - 如何创建一个在设备打开时设置为默认值的 Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34505004/