Why my widget doesn't notice when Screen turn on? - android

I developed an Android Widget like this:
<receiver android:name=".MyWidget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="android.intent.action.ACTION_SCREEN_ON" />
<action android:name="android.intent.action.ACTION_SCREEN_OFF" />
</intent-filter>
In my widget
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
playNotification(context, true);
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
playNotification(context, true);
}
}
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
[...]
The updateAppWidget works perfectly. The notification instead doesn't work neither if the screen is turned on, nor if it is turned off.
What's wrong with that?

This is because
Intent.ACTION_SCREEN_OFF = "android.intent.action.SCREEN_OFF" // not ACTION_SCREEN_OFF
Intent.ACTION_SCREEN_ON = "android.intent.action.SCREEN_ON" // not ACTION_SCREEN_ON
When you fix the intent filter in manifest, everything should work fine.

Related

Can't click on Android widget after app process dies

I've coded an Android widget with two buttons. If i start the "Activity" all works fine and i can click on those buttons. But if i lock the phone, the process dies after a few seconds
and i try to click on those buttons again, nothing happens, except the LOGCAT:
I/ActivityManager: filter receiver for action = ButtonRefresh
updateAppWidget, called via #Override onUpdate(...):
private final static String BUTTON_REFRESH = "ButtonRefresh";
public static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager,
final int appWidgetId) {
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.myclasslayout);
//Click listener depending on #this (getPendingSelfIntent)
views.setOnClickPendingIntent(R.id.button_refresh, getPendingSelfIntent(context, BUTTON_REFRESH));
[...]
appWidgetManager.updateAppWidget(appWidgetId, views);
}
getPendingSelfIntent():
private static PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, MyClass.class);
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
onReceive():
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (BUTTON_REFRESH.equals(intent.getAction())) {
Log.i("MyLogtag", "Refresh"); //This never shows up in LOGCAT
}
}
AndroidManifest.xml:
[...]
<receiver android:name=".MyClass">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/myclass_info" />
</receiver>
[...]
onReceive never gets called when app process is dead...
I got it...
i have to change
private final static String BUTTON_REFRESH = "ButtonRefresh";
to
private final static String BUTTON_REFRESH = "com.example.mypackage.BUTTON_REFRESH";
and AndroidManifest:
<receiver android:name=".MyClass">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.example.mypackage.BUTTON_REFRESH"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/myclass_info" />
</receiver>

Android: Widget onReceive() not getting called

I have a widget whose layout has an imageView. The view needs to be changed based on some activity. So, whenever that happens, I am calling a sendBroadcast() with the action set to UPDATE_WIDGET. However, the onReceive() in the widget code is not getting called. Below is my code.
public class TaskWidget extends AppWidgetProvider {
...
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "widget update", 2000).show();
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.searchwidget);
if (intent.getAction().equals(UPDATE_WIDGET)) {
views.setImageViewResource(R.id.newImage, R.drawable.Pic1);
}
super.onReceive(context, intent);
}
}
In the manifest,
<receiver
android:name="com.rahul.testwidget.TaskWidget"
android:label="Task Widget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.rahul.UPDATE_WIDGET" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget" />
</receiver>
you have to update your widget layout
use this code just befor super.onReceive(context, intent);
ComponentName widget = new ComponentName(context, SimpleWidget.class);
AppWidgetManager.getInstance(context).updateAppWidget(widget, views);
I think your action name must be com.rahul.UPDATE_WIDGET instead of UPDATE_WIDGET.
If you are sending the broadcast from a listener on your widget you need to create a PendingIntent for a Broadcast.
PendingIntent sentPI = PendingIntent.getBroadcast(activity, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
Also, you don't seem to be able to attach 2 pending intents to a button in the widget.

Enabling / Disabling BroadcastReceivers

I have a broadcast receiver that starts a service when the device is finished booting. However I don't know how to disable/enable it in code.
Here is my Manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:name=".BootReceiver"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
<action android:name="android.intent.action.QUICKBOOT_POWERON">
</intent-filter>
</receiver>
BootReceiver.java:
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
context.startService(new Intent(context, TouchService.class));
}
}
My question is, what do I put here?
if (!rBoot.isChecked()) {
????????????
}
What you can do is use SharedPreference and set a flag when your device completes boot and check the flag for enabling and disabling your BroadCastReceiver.
Try this
boolean enabled=prefs.getBoolean(key, false);
int flag=(enabled ?
PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
ComponentName component=new ComponentName(EditPreferences.this, OnBootReceiver.class);
getPackageManager()
.setComponentEnabledSetting(component, flag,
PackageManager.DONT_KILL_APP);

BroadcastEvent, alarm function android

I have run into a snag, I can't get my alarms properly setup again after a boot.
public void onReceive(Context context, Intent intent) {
if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){
if(fm.getAlarmBool()){
time = fm.getAlarmTimeLong();
reminder.startAlarm(time);
}
}
This is my method that I want to run after it boots up. I have added permissions in the android manifest but I can't get it to work. Whats wrong?
You should do it like
public class BootReceiver extends BroadcastReceiver {
Context context;
#Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, NotificationService.class));
}
}
in manifest
<receiver android:process=":remote" android:name="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</receiver>
so here in your service collect all the reminders and reschedule them.
for this you can store it in shared pref or database for persistent storage

Using sendBroadcast for update android widgets

I am creating a appwidget which needs to update when some changing in app's database.
What I do:
1. Create working widget
2. Override onReceive method:
public static final String DATABASE_CHANGED = " utimetable.DATABASE_CHANGED";
#Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (action.equals(DATABASE_CHANGED) || action.equals(Intent.ACTION_DATE_CHANGED))
{
AppWidgetManager gm = AppWidgetManager.getInstance(context);
int[] ids = gm.getAppWidgetIds(new ComponentName(context, widget_provider.class));
this.onUpdate(context, gm, ids);
}
else
{
super.onReceive(context, intent);
}
}
In AndroidManifest:
<receiver android:name=".widget_provider" android:label="#string/widget_today_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="utimetable.DATABASE_CHANGED" />
<action android:name="android.intent.action.DATE_CHANGED" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="#xml/widget_row" />
</receiver>
Add Broadcast sending into all DB changing functions:
public static void sendUpdateIntent(Context context)
{
Intent i = new Intent(context, widget_provider.class);
i.setAction(widget_provider.DATABASE_CHANGED);
context.sendBroadcast(i);
}
But widget still not updated when I make changes in DB.
What should I do?
just guessing looking at your code, but that space after the first " looks wrong
.... DATABASE_CHANGED = " utimetable.DATABASE_CHANGED";

Categories

Resources