我有一个安排工作的小部件,但我得到了结果:JobScheduler.schedule(job)的RESULT_SUCCESS,但是我的服务永不启动。我等了30多分钟。
Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tac.widgetproject">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver android:name=".WidgetOne"
android:icon="@drawable/ic_launcher_background"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_provider"/>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service android:name=".ServiceWidgetOne"
android:permission="android.permission.BIND_JOB_SERVICE"/>
</application>
WidgetOne类(kotlin)
import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.util.Log
import android.widget.RemoteViews
import java.text.DateFormat
import java.util.*
class WidgetOne : AppWidgetProvider() {
companion object {
const val TAG = "Widget"
// static method
fun getCallingPendingIntent(context: Context): PendingIntent {
val manager = AppWidgetManager.getInstance(context)
val componentName = ComponentName(context, ServiceWidgetOne::class.java)
val ids = manager.getAppWidgetIds(componentName)
val updateIntent = Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE)
.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
return PendingIntent.getBroadcast(context, 1, updateIntent, PendingIntent.FLAG_UPDATE_CURRENT)
}
}
override fun onEnabled(context: Context) {
super.onEnabled(context)
ServiceWidgetOne.manageJobScheduler(context, true)
}
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray?) {
super.onUpdate(context, appWidgetManager, appWidgetIds)
Log.i(TAG, "onUpdate()")
val views = RemoteViews(context.packageName, R.layout.widget_one)
val savedTime = Prefs().getLastNewsDate(context)
val timeText = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT).format(Date(savedTime))
views.setTextViewText(R.id.tvTime, timeText)
}
override fun onDisabled(context: Context) {
super.onDisabled(context)
Prefs().saveLastNewsDate(context, 0)
ServiceWidgetOne.manageJobScheduler(context, false)
}
}
ServiceWidgetOne类(kotlin)
import android.annotation.TargetApi
import android.app.AlarmManager
import android.app.job.JobInfo
import android.app.job.JobScheduler
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Build.VERSION_CODES.N
import android.support.annotation.RequiresApi
import android.support.v4.app.JobIntentService
import android.util.Log
@RequiresApi(Build.VERSION_CODES.N)
class ServiceWidgetOne : JobIntentService() {
companion object {
private const val TAG = "ServiceWidgetOne"
private const val TIME_INTERVAL = AlarmManager.INTERVAL_FIFTEEN_MINUTES
private const val JOB_ID = 65276
@TargetApi(N) // static method
fun manageJobScheduler(context: Context, isOn: Boolean) {
val jobScheduler = context.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
if (isOn) {
val componentName = ComponentName(context, ServiceWidgetOne::class.java)
val job = JobInfo.Builder(JOB_ID, componentName)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setPeriodic(TIME_INTERVAL)
.build()
val jobResult = jobScheduler.schedule(job)
Log.i(TAG, "Job #$JOB_ID activated: ${if (jobResult == JobScheduler.RESULT_SUCCESS)
"successful" else "with Error"}")
} else {
jobScheduler.cancel(JOB_ID)
Log.i(TAG, "Job #$JOB_ID canceled")
}
}
}
override fun onHandleWork(intent: Intent) {
Log.i(TAG, "onHandleWork")
}
}
在MainActivity中,我什么也不做。 当我在日志中将小部件放到主屏幕时,我有:
I/ServiceWidgetOne: Job #65276 activated: successful
仅此而已:(我的服务永远不会启动
我在清单中尝试过
android:enabled="true"
android:exported="true"
但是没有效果 感谢您的帮助!