无论我做什么,我都无法从我的活动中获得额外的服务!这对我来说至关重要,我在这一点上非常沮丧,我已经尝试了各种类型的旗帜!请帮助!我正在使用PendingIntent并尝试将Extras从intent传递给pendingIntent,然后再传递给服务。
This is my code for the PendingIntent part.
public void actionSetup(boolean activitycreated[],int buttonclicked[])
{
SharedPreferences data=getSharedPreferences("ACTIVITY_DATA",MODE_MULTI_PROCESS);
Calendar calendar=Calendar.getInstance();
int origin=Integer.valueOf(buttonclicked[0]);
myintent[buttonclicked[0]]=new Intent(getApplicationContext(),Handler.class);
myintent[buttonclicked[0]].setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
myintent[buttonclicked[0]].setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
myintent[buttonclicked[0]].putExtra("origin",origin);
myintent[buttonclicked[0]].setAction(String.valueOf(origin));
stuff[buttonclicked[0]]=PendingIntent.getBroadcast(getApplicationContext(),buttonclicked[0],myintent[buttonclicked[0]],PendingIntent.FLAG_CANCEL_CURRENT);
calendar.set(Calendar.MONTH,Calendar.getInstance().get(Calendar.MONTH));
calendar.set(Calendar.YEAR,Calendar.getInstance().get(Calendar.YEAR));
calendar.set(Calendar.DAY_OF_MONTH,Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
calendar.set(Calendar.HOUR_OF_DAY, data.getInt(buttonclicked[0]+"startimehour",1));
calendar.set(Calendar.MINUTE, data.getInt(buttonclicked[0]+"startimeminute",1));
calendar.set(Calendar.SECOND,0);
if (data.getInt(buttonclicked[0]+"startimehour",1)>=12)
{
calendar.set(Calendar.AM_PM,Calendar.PM);
}
else
{
calendar.set(Calendar.AM_PM,Calendar.AM);
}
AlarmManager alarmManager= (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(alarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),stuff[buttonclicked[0]]);
Log.i("value of da origin",String.valueOf(origin));
}
}
服务代码
public int onStartCommand(Intent intent, int flags, int startId)
{
Bundle exras=intent.getExtras();
int origin=exras.getInt("origin");
if(exras !=null ) {
Log.i("AY", String.valueOf(origin));
}
SharedPreferences data=getSharedPreferences("ACTIVITY_DATA",MODE_MULTI_PROCESS);
boolean daysselected [] []=new boolean[8][7];
boolean [] [] functionsselected=new boolean[8][2];
for(int m=0;m<=7;m++)
{
functionsselected[m][0]=data.getBoolean("silent"+m,false);
functionsselected[m][1]=data.getBoolean("bluetooth"+m,false);
}
Calendar c= Calendar.getInstance();
int dayofweek=c.get(Calendar.DAY_OF_WEEK);
Handler.completeWakefulIntent(intent);
stopSelf();
return super.onStartCommand(intent, flags, startId);
}