Gad*_*ady 96 java android broadcastreceiver intentservice
从我在Stack Exchange和其他地方看到的所有东西,我已经正确设置了所有东西,以便在Android OS启动时启动IntentService.不幸的是它没有启动,我没有收到任何错误.也许专家可以提供帮助......
表现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.phx.batterylogger"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="internalOnly">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BATTERY_STATS" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<service android:name=".BatteryLogger"/>
<receiver android:name=".StartupIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
BroadcastReceiver for Startup:
package com.phx.batterylogger;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class StartupIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context, BatteryLogger.class);
context.startService(serviceIntent);
}
}
Run Code Online (Sandbox Code Playgroud)
更新:我尝试了下面的所有建议,并且我将日志记录添加Log.v("BatteryLogger", "Got to onReceive, about to start service");到StartupIntentReceiver的onReceive处理程序中,并且没有记录任何内容.所以它甚至没有进入BroadcastReceiver.
我想我正在部署APK并正确测试,只需在Eclipse中运行Debug,控制台就说它成功将它安装到我的Xoom平板电脑\ BatteryLogger\bin\BatteryLogger.apk.然后进行测试,我重新启动平板电脑,然后查看DDMS中的日志,并在操作系统设置中检查正在运行的服务.这一切听起来都是正确的,还是我错过了什么?再次,非常感谢任何帮助.
Lal*_*ani 280
这是一个AutoStart应用程序的完整示例
AndroidManifest文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pack.saltriver" android:versionCode="1" android:versionName="1.0">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name=".autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity android:name=".hello"></activity>
<service android:enabled="true" android:name=".service" />
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
autostart.java
public class autostart extends BroadcastReceiver
{
public void onReceive(Context context, Intent arg1)
{
Intent intent = new Intent(context,service.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
Log.i("Autostart", "started");
}
}
Run Code Online (Sandbox Code Playgroud)
service.java
public class service 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(),hello.class);
intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intents);
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}
}
Run Code Online (Sandbox Code Playgroud)
hello.java - 执行Applicaton一次后,每次启动设备时都会弹出.
public class hello extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(getBaseContext(), "Hello........", Toast.LENGTH_LONG).show();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
97020 次 |
| 最近记录: |