Service with intents not working. Help needed
Posted
by
tristan202
on Stack Overflow
See other posts from Stack Overflow
or by tristan202
Published on 2011-01-03T15:50:17Z
Indexed on
2011/01/03
15:54 UTC
Read the original article
Hit count: 263
I need help in making my click intents work. I used to have them in my appwidgetprovider, but decided to move them into a service, but I am having trouble getting it to work. Below is the entire code from my intentservice:
public class IntentService extends Service {
static final String ACTION_UPDATE = "android.tristan.widget.digiclock.action.UPDATE_2";
private final static IntentFilter sIntentFilter;
public int layoutID = R.layout.clock;
int appWidgetIds = 0;
static {
sIntentFilter = new IntentFilter();
}
@Override public IBinder onBind(Intent intent) { return null; }
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
@Override
public void onCreate() {
super.onCreate();
registerReceiver(onClickTop, sIntentFilter);
registerReceiver(onClickBottom, sIntentFilter);
Log.d("DigiClock IntentService", "IntentService Started.");
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(onClickTop);
unregisterReceiver(onClickBottom);
}
private final BroadcastReceiver onClickTop = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)
{
if(intent.getAction().equals("android.tristan.widget.digiclock.CLICK"))
{
PackageManager packageManager = context.getPackageManager();
Intent alarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
String clockImpls[][] = {
{"HTC Alarm Clock", "com.htc.android.worldclock", "com.htc.android.worldclock.WorldClockTabControl" },
{"Standar Alarm Clock", "com.android.deskclock", "com.android.deskclock.AlarmClock"},
{"Froyo Nexus Alarm Clock", "com.google.android.deskclock", "com.android.deskclock.DeskClock"},
{"Moto Blur Alarm Clock", "com.motorola.blur.alarmclock", "com.motorola.blur.alarmclock.AlarmClock"}
};
boolean foundClockImpl = false;
for(int i=0; i<clockImpls.length; i++) {
String vendor = clockImpls[i][0];
String packageName = clockImpls[i][1];
String className = clockImpls[i][2];
try {
ComponentName cn = new ComponentName(packageName, className);
ActivityInfo aInfo = packageManager.getActivityInfo(cn, PackageManager.GET_META_DATA);
alarmClockIntent.setComponent(cn);
foundClockImpl = true;
} catch (NameNotFoundException e) {
Log.d("Error, ", vendor + " does not exist");
}
}
if (foundClockImpl) {
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(50);
final RemoteViews views = new RemoteViews(context.getPackageName(), layoutID);
views.setOnClickPendingIntent(R.id.TopRow, PendingIntent.getActivity(context, 0, new Intent(context, DigiClock.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK), PendingIntent.FLAG_UPDATE_CURRENT));
AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views);
alarmClockIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(alarmClockIntent);
}
}
}
};
private final BroadcastReceiver onClickBottom = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)
{
if(intent.getAction().equals("android.tristan.widget.digiclock.CLICK_2"))
{
PackageManager calendarManager = context.getPackageManager();
Intent calendarIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
String calendarImpls[][] = {
{"HTC Calendar", "com.htc.calendar", "com.htc.calendar.LaunchActivity" },
{"Standard Calendar", "com.android.calendar", "com.android.calendar.LaunchActivity"},
{"Moto Blur Calendar", "com.motorola.blur.calendar", "com.motorola.blur.calendar.LaunchActivity"}
};
boolean foundCalendarImpl = false;
for(int i=0; i<calendarImpls.length; i++) {
String vendor = calendarImpls[i][0];
String packageName = calendarImpls[i][1];
String className = calendarImpls[i][2];
try {
ComponentName cn = new ComponentName(packageName, className);
ActivityInfo aInfo = calendarManager.getActivityInfo(cn, PackageManager.GET_META_DATA);
calendarIntent.setComponent(cn);
foundCalendarImpl = true;
} catch (NameNotFoundException e) {
Log.d("Error, ", vendor + " does not exist");
}
}
if (foundCalendarImpl) {
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(50);
final RemoteViews views2 = new RemoteViews(context.getPackageName(), layoutID);
views2.setOnClickPendingIntent(R.id.BottomRow, PendingIntent.getActivity(context, 0, new Intent(context, DigiClock.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK), PendingIntent.FLAG_UPDATE_CURRENT));
AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views2);
calendarIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(calendarIntent);
}
}
}; }; ;};
What am I doing wrong here?
© Stack Overflow or respective owner