我正在尝试将一项服务组装在一起,该服务仅在“启动”时发出通知,然后在服务运行后发出另一个通知。我在清单中使用以下内容:
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="my.project"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17"
/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<service
android:name=".Manager"
/>
<receiver
android:name=".Bootstrap"
>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
不幸的是,当我重新启动设备时,什么都没有显示,而且似乎该服务从未启动。
这是我的BroadcastReceiver
子类的实现:
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Test", "Bootstrap run!");
int notificationId = 1;
NotificationCompat.Builder startNotification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Glance")
.setContentText("Starting the Glance service.")
;
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, startNotification.build());
//
// Run the service.
//
Intent startServiceIntent = new Intent(context, Manager.class);
context.startService(startServiceIntent);
}
关于我在这里可能做错或遗漏的任何建议导致事情无法正常工作?
谢谢!