启动完成后,我无需加载消息(日志)。我尝试使用以下代码:
在BootReceiver.java文件中:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Booting Completed", Toast.LENGTH_LONG).show();
Log.d("On Boot Load", "Boot loading");
}
}
在Services.java文件中:
public class UpdateService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d("Service", "Updating Services");
return null;
}
}
我还在menifes.xml中添加了以下代码:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".receivers.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
当我运行该应用程序时,日志不会出现在logcat部分中。
答案 0 :(得分:0)
您应使用onStartCommand()
实现:
public class UpdateService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d("Service", "Updating Services");
return null;
}
@Override
public int onStartCommand(Intent pIntent, int flags, int startId) {
Log.d("On Boot Load", "Boot loading");
return super.onStartCommand(pIntent, flags, startId);
}
}
还要提防android.intent.action.QUICKBOOT_POWERON
之类的其他过滤器。
我使用此代码(不提供服务)并且对我有用(刚刚对其进行了测试):
AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".MyBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
科特琳:
class MyBootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == ACTION) {
Logger.warning("BOOTING UP!")
}
}
companion object {
const val ACTION = "android.intent.action.BOOT_COMPLETED"
}
}
答案 1 :(得分:0)
接收方标签应在应用程序标签内,而使用权限标签应在应用程序标签外但清单标签内。