开发者

unable to start service intent?

开发者 https://www.devze.com 2023-02-15 09:12 出处:网络
i have three classess.activity,service and appwidget. i have two buttons in appwidget. and onclicking of that button widget should be updated as well methods in my activity should be called, in my act

i have three classess.activity,service and appwidget. i have two buttons in appwidget. and onclicking of that button widget should be updated as well methods in my activity should be called, in my activity there are some buttons,when i click buttons in my activity widget should be updated.

In this some code is unused.

here is my code.

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;
    }

}

and here is my oncreate method code.

  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);

my question: 1.why i am getting this error "unable to start service intent" 2.i simply want to update widget when in my activity button is clicked and when i click on widget buttons widget should be updated and method should be called from service class.

here is my log.

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


Take a look at http://programmerbruce.blogspot.com/2011/04/simple-complete-app-widget-part-1.html for a complete, working example of an App Widget successfully using intents to launch an activity and App Widget updates.

If you're still stuck, then it may help folks to help you, if you were to post complete minimal code - including all XML - to replicate your problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号