Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法启动服务意图?_Android - Fatal编程技术网

Android 无法启动服务意图?

Android 无法启动服务意图?,android,Android,我有三个类:活动、服务和应用程序。我在appwidget中有两个按钮。 一旦点击按钮小部件,就会更新,我的活动中的方法也会被调用,在我的活动中有一些按钮,当我点击我的活动小部件中的按钮时,就会更新 在这种情况下,有些代码未使用。 这是我的密码 public class MediaAppWidgetProvider extends AppWidgetProvider { //update rate in milliseconds public static final int U

我有三个类:活动、服务和应用程序。我在appwidget中有两个按钮。 一旦点击按钮小部件,就会更新,我的活动中的方法也会被调用,在我的活动中有一些按钮,当我点击我的活动小部件中的按钮时,就会更新

在这种情况下,有些代码未使用。

这是我的密码

public class MediaAppWidgetProvider extends AppWidgetProvider {

    //update rate in milliseconds
    public static final int UPDATE_RATE = 1000;
    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        for (int appWidgetId : appWidgetIds) {       
            setAlarm(context, appWidgetId, -1);
        }
        super.onDeleted(context, appWidgetIds);
    }

    @Override
    public void onDisabled(Context context) {
        context.stopService(new Intent(context,BackService.class));
        super.onDisabled(context);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        for (int appWidgetId : appWidgetIds) {
            setAlarm(context, appWidgetId, UPDATE_RATE);
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

    public static void setAlarm(Context context, int appWidgetId, int updateRate) {
        PendingIntent newPending = makeControlPendingIntent(context,BackService.UPDATE,appWidgetId);
        AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if (updateRate >= 0) {
            alarms.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), updateRate, newPending);
        } else {
            // on a negative updateRate stop the refreshing 
            alarms.cancel(newPending);
        }
    }

    public static PendingIntent makeControlPendingIntent(Context context, String command, int appWidgetId) {
        Intent active = new Intent(context,BackService.class);
        active.setAction(command);
        active.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        //this Uri data is to make the PendingIntent unique, so it wont be updated by FLAG_UPDATE_CURRENT
        //so if there are multiple widget instances they wont override each other
        Uri data = Uri.withAppendedPath(Uri.parse("MediaAppWidgetProvider://widget/id/#"+command+appWidgetId), String.valueOf(appWidgetId));
        active.setData(data);
        return(PendingIntent.getService(context, 0, active, PendingIntent.FLAG_UPDATE_CURRENT));
    }
}




public class BackService extends Service{

    public static final String UPDATE = "update";
    public static final String PLUS = "plus";
    public static final String MINUS = "minus";
    public static final long  MODIFY= 86400000;

    @Override
    public void onStart(Intent intent, int startId) {
        String command = intent.getAction();
        int appWidgetId = intent.getExtras().getInt(
                AppWidgetManager.EXTRA_APPWIDGET_ID);
        RemoteViews remoteView = new RemoteViews(getApplicationContext()
                .getPackageName(), R.layout.album_appwidget);
        AppWidgetManager appWidgetManager = AppWidgetManager
                .getInstance(getApplicationContext());
        SharedPreferences prefs = getApplicationContext().getSharedPreferences(
                "prefs", 0);


        //plus button pressed
        if(command.equals(PLUS)){
            SharedPreferences.Editor edit=prefs.edit();
            edit.putLong("goal" + appWidgetId,prefs.getLong("goal" + appWidgetId, 0)+MODIFY);
            edit.commit();

        //minus button pressed
        }else if(command.equals(MINUS)){
            SharedPreferences.Editor edit=prefs.edit();
            edit.putLong("goal" + appWidgetId,prefs.getLong("goal" + appWidgetId, 0)-MODIFY);
            edit.commit();
        }


        long goal = prefs.getLong("goal" + appWidgetId, 0);
        //compute the time left
        long left = goal - new Date().getTime();
        int days = (int) Math.floor(left / (long) (60 * 60 * 24 * 1000));
        left = left - days * (long) (60 * 60 * 24 * 1000);
        int hours = (int) Math.floor(left / (60 * 60 * 1000));
        left = left - hours * (long) (60 * 60 * 1000);
        int mins = (int) Math.floor(left / (60 * 1000));
        left = left - mins * (long) (60 * 1000);
        int secs = (int) Math.floor(left / (1000));
        //put the text into the textView
        remoteView.setTextViewText(R.id.title, days + " days\n" + hours
                + " hours " + mins + " mins " + secs + " secs left");
        //set buttons
        remoteView.setOnClickPendingIntent(R.id.control_play,MediaAppWidgetProvider.makeControlPendingIntent(getApplicationContext(),PLUS,appWidgetId));
        remoteView.setOnClickPendingIntent(R.id.control_next,MediaAppWidgetProvider.makeControlPendingIntent(getApplicationContext(),MINUS,appWidgetId));
        // apply changes to widget
        appWidgetManager.updateAppWidget(appWidgetId, remoteView);
        super.onStart(intent, startId);
    }
    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

}
这是我的oncreate方法代码

  Intent launchIntent = getIntent();
        Bundle extras = launchIntent.getExtras();
        appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);

        // set the result for cancel first
        Intent cancelResultValue = new Intent();
        cancelResultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                appWidgetId);
        setResult(RESULT_CANCELED, cancelResultValue);

        SharedPreferences prefs = self.getSharedPreferences("prefs", 0);
        SharedPreferences.Editor edit = prefs.edit();
        edit.putLong("goal" + appWidgetId, 0l);
        edit.commit();
        // fire an update to display initial state of the widget
        PendingIntent updatepending = MediaAppWidgetProvider
                .makeControlPendingIntent(self,
                        BackService.UPDATE, appWidgetId);
        try {
            updatepending.send();
        } catch (CanceledException e) {
            e.printStackTrace();
        }
        // change the result to OK
        Intent resultValue = new Intent();
        resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                appWidgetId);
        setResult(RESULT_OK, resultValue);
我的问题: 1.我为什么会出现“无法启动服务意图”错误 2.我只想在我的活动中单击按钮时更新小部件,当我单击小部件按钮时,小部件应该更新,并且应该从服务类调用方法

这是我的日志

ain/com.streamingmusic.widget.BackService (has extras) }: not found
W/AudioFlinger(   31): write blocked for 91 msecs
W/AudioFlinger(   31): write blocked for 66 msecs
W/AudioFlinger(   31): write blocked for 95 msecs
W/AudioFlinger(   31): write blocked for 67 msecs
D/PlayerDriver(   31): buffering (96)
W/AudioFlinger(   31): write blocked for 90 msecs
W/AudioFlinger(   31): write blocked for 69 msecs
W/AudioFlinger(   31): write blocked for 101 msecs
W/AudioFlinger(   31): write blocked for 79 msecs
W/AudioFlinger(   31): write blocked for 82 msecs
W/ActivityManager(   52): Unable to start service Intent { act=update dat=MediaAppWidgetProvider://widget/id/25#update25 flg=0x4 cmp=org.streamingmusic.m
ain/com.streamingmusic.widget.BackService (has extras) }: not found
W/AudioFlinger(   31): write blocked for 80 msecs
W/AudioFlinger(   31): write blocked for 66 msecs
W/AudioFlinger(   31): write blocked for 89 msecs
W/AudioFlinger(   31): write blocked for 67 msecs
W/AudioFlinger(   31): write blocked for 91 msecs
W/AudioFlinger(   31): write blocked for 68 msecs
W/AudioFlinger(   31): write blocked for 87 msecs
W/AudioFlinger(   31): write blocked for 70 msecs
D/PlayerDriver(   31): buffering (97)
W/AudioFlinger(   31): write blocked for 65 msecs
W/ActivityManager(   52): Unable to start service Intent { act=update dat=MediaAppWidgetProvider://widget/id/25#update25 flg=0x4 cmp=org.streamingmusic.m
ain/com.streamingmusic.widget.BackService (has extras) }: not found
W/AudioFlinger(   31): write blocked for 97 msecs
W/AudioFlinger(   31): write blocked for 70 msecs
W/AudioFlinger(   31): write blocked for 88 msecs
W/AudioFlinger(   31): write blocked for 68 msecs
W/AudioFlinger(   31): write blocked for 79 msecs
W/AudioFlinger(   31): write blocked for 79 msecs
W/AudioFlinger(   31): write blocked for 72 msecs
W/ActivityManager(   52): Unable to start service Intent { act=update dat=MediaAppWidgetProvider://widget/id/25#update25 flg=0x4 cmp=org.streamingmusic.m
ain/com.streamingmusic.widget.BackService (has extras) }: not found
W/AudioFlinger(   31): write blocked for 132 msecs
W/AudioFlinger(   31): write blocked for 89 msecs
W/AudioFlinger(   31): write blocked for 72 msecs
W/AudioFlinger(   31): write blocked for 77 msecs
D/PlayerDriver(   31): buffering (97)
W/AudioFlinger(   31): write blocked for 80 msecs
W/AudioFlinger(   31): write blocked for 87 msecs
W/AudioFlinger(   31): write blocked for 68 msecs
W/AudioFlinger(   31): write blocked for 92 msecs
W/AudioFlinger(   31): write blocked for 69 msecs
W/ActivityManager(   52): Unable to start service Intent { act=update dat=MediaAppWidgetProvider://widget/id/25#update25 flg=0x4 cmp=org.streamingmusic.m
ain/com.streamingmusic.widget.BackService (has extras) }: not found
W/AudioFlinger(   31): write blocked for 93 msecs
W/AudioFlinger(   31): write blocked for 66 msecs
W/AudioFlinger(   31): write blocked for 86 msecs
W/AudioFlinger(   31): write blocked for 73 msecs
W/AudioFlinger(   31): write blocked for 72 msecs
W/AudioFlinger(   31): write blocked for 88 msecs
W/AudioFlinger(   31): write blocked for 68 msecs
W/AudioFlinger(   31): write blocked for 96 msecs
W/AudioFlinger(   31): write blocked for 69 msecs
W/ActivityManager(   52): Unable to start service Intent { act=update dat=MediaAppWidgetProvider://widget/id/25#update25 flg=0x4 cmp=org.streamingmusic.m
ain/com.streamingmusic.widget.BackService (has extras) }: not found
W/AudioFlinger(   31): write blocked for 103 msecs
W/AudioFlinger(   31): write blocked for 83 msecs
W/AudioFlinger(   31): write blocked for 67 msecs
D/PlayerDriver(   31): buffering (97)
W/AudioFlinger(   31): write blocked for 90 msecs
W/AudioFlinger(   31): write blocked for 80 msecs
D/PlayerDriver(   31): buffering (98)
W/AudioFlinger(   31): write blocked for 72 msecs
W/AudioFlinger(   31): write blocked for 66 msecs
W/AudioFlinger(   31): write blocked for 90 msecs
W/ActivityManager(   52): Unable to start service Intent { act=update dat=MediaAppWidgetProvider://widget/id/25#update25 flg=0x4 cmp=org.streamingmusic.m
ain/com.streamingmusic.widget.BackService (has extras) }: not found
W/AudioFlinger(   31): write blocked for 93 msecs
W/AudioFlinger(   31): write blocked for 91 msecs
W/AudioFlinger(   31): write blocked for 68 msecs
W/AudioFlinger(   31): write blocked for 66 msecs
W/AudioFlinger(   31): write blocked for 96 msecs
W/AudioFlinger(   31): write blocked for 88 msecs
thankx

查看一个完整的应用程序小部件工作示例,该小部件成功地使用意图启动活动和应用程序小部件更新

如果您仍处于困境,那么,如果您发布完整的最小代码(包括所有XML)来复制您的问题,那么它可能会帮助人们帮助您。

请查看一个完整的应用程序小部件工作示例,它成功地使用了启动活动和应用程序小部件更新的意图

如果您仍然陷于困境,那么如果您发布完整的最小代码(包括所有XML)来复制您的问题,它可能会帮助人们帮助您