Android AppwidgetProvider never called - android

I'm following this http://www.vogella.com/articles/AndroidWidgets/article.html tutorial.
Heres my Widetprovider:
import java.util.Random;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import com.asco.countdown.R;
public class MyWidgetProvider extends AppWidgetProvider {
private static final String ACTION_CLICK = "ACTION_CLICK";
#Override
public void onEnabled(Context context) {
Log.d("WID", "enabled");
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Log.d("WID", "update");
// Get all ids
ComponentName thisWidget = new ComponentName(context,
MyWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
// Create some random data
int number = (new Random().nextInt(100));
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget);
Log.w("WidgetExample", String.valueOf(number));
// Set the text
remoteViews.setTextViewText(R.id.update, String.valueOf(number));
// Register an onClickListener
Intent intent = new Intent(context, MyWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
}
Heres my widgetinfo.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="#layout/widget"
android:minHeight="72dp"
android:minWidth="300dp"
android:updatePeriodMillis="5000000" >
</appwidget-provider>
and heres my manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver android:name="MyWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
<action android:name="android.appwidget.action.APPWIDGET_DELETED" />
<action android:name="android.appwidget.action.APPWIDGET_DISABLED" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widgetinfo" />
</receiver>
</application>
The widget is displayed, but neither the onEnabled() nor the onUpdate() methods are called, the onClickListener doesnt work either.
I played around with giving the full path to the AppWidgetProvider in the manifest, putting the AppWidgetProvider in another package (right now its in the default package) and so on, but nothing happens, and now I'm out of ideas. Can someone help me out?

Try giving full name in the maifest
<receiver android:name="MyWidgetProvider" >
to
<receiver android:name="com.asco.countdown.MyWidgetProvider" >
or what ever the full name

Okay, tested it on a 4.0.3 emulator and 2.3 device, both worked fine. Seems to be a problem with my custom 4.1 JB rom.

Related

Widget loads as app

I'm trying to build a hello world widget and I followed three different examples and every time my apk loads the main activity as an app and not a widget. In the last example it showed a manifest with no activity block. When I take the main activity out of the manifest and just have the receiver block Android Studio throws an exception of Default activity not found. So that is the one difference I have from the example. I have an <*activity> block in the manifest.
I'm in Android Studio 1.0.2. What could be causing this?
My current code is based on this example
http://www.vogella.com/tutorials/AndroidWidgets/article.html
http://www.sitepoint.com/how-to-code-an-android-widget/
https://looksok.wordpress.com/2012/12/15/android-complete-widget-tutorial-including-source-code/
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.me.countdown" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="MyWidgetProvider" >
<intent-filter >
<action
android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget_info" />
</receiver>
</application>
</manifest>
MyWidgetProvider.xml
public class MyWidgetProvider extends AppWidgetProvider {
private static final String ACTION_CLICK = "ACTION_CLICK";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// Get all ids
ComponentName thisWidget = new ComponentName(context,
MyWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
// create some random data
int number = (new Random().nextInt(100));
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
Log.w("WidgetExample", String.valueOf(number));
// Set the text
remoteViews.setTextViewText(R.id.update, String.valueOf(number));
// Register an onClickListener
Intent intent = new Intent(context, MyWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
}
widget_info.xml under res/xml
\<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="#layout/widget_layout"
android:minHeight="72dp"
android:minWidth="300dp"
android:updatePeriodMillis="300000" >
</appwidget-provider>
I'm not sure what those other examples are designed for, but the version of Android Studio I'm using - 1.2.2 - does it differently. I found the example below and was able to get a working shell of a widget, which is what I wanted. Even this one needed many small tweaks before it would compile and run, but I did get it.
Greg
http://www.clearcreekcode.com/create-a-widget-in-android-studio/

Widget doesn show on emulator or devices and no errors showing

I'm new to android development. At the moment I'm trying to write an app widget. I've done a few widget tutorials and when I go to run them the app will run but the widget isn't showing up on the widget list to be used. Widgets that are not extensions of an app still will run as if they are an app and will not show up in the widgets list. This is happening with all the widgets iv tried to run. No errors are showing. Somebody please help its driving me mad. Thanks
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.somethinginspiring"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.somethinginspiring.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- declaring widget -->
<receiver android:name="AppWidgetProv"
android:label="#string/app_name"
android:icon="#drawable/ic_launcher">
<intent-filter >
<action
android:name="android.appwidget.action.APPWIGET_UPDATE"/>
</intent-filter>
<!-- declaring the widget_info -->
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget_info"/>
</receiver>
<!-- declaring broadcaster -->
<receiver
android:name="WidgetIntentReceiver"
android:label="WidgetBroadcastReceiver" >
<intent-filter>
<action android:name="android.appwidget.intent.action.CHANGE_PICTURE"/>
</intent-filter>
<!-- declaring meta-data -->
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget_info"/>
</receiver>
</application>
</manifest>
widget info xml file
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="#layout/widget_layout"
android:minHeight="72dp"
android:minWidth="300dp"
android:widgetCategory="home_screen"
android:minResizeHeight="72dp"
android:minResizeWidth="300dp"
android:updatePeriodMillis="2000000">
<!-- android:updatePerMillis="3000000" -->
</appwidget-provider>
widget layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/widget_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/image_view_text"
android:clickable="true"
android:src="#drawable/j_1" >
</ImageView>
</LinearLayout>
app widget provider
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class AppWidgetProv extends AppWidgetProvider {
#Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
super.onEnabled(context);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
remoteView.setOnClickPendingIntent(R.id.widget_image_view, buildButtonPendingIntent(context));
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
public PendingIntent buildButtonPendingIntent(Context context) {
Intent changePicture = new Intent();
changePicture.setAction("android.widget.intent.action.CHANGE_PICTURE");
return PendingIntent.getBroadcast(context, 0, changePicture, PendingIntent.FLAG_UPDATE_CURRENT);
}
public static void pushWidgetUpdate(Context context, RemoteViews remoteView){
ComponentName widget = new ComponentName(context, AppWidgetProv.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(widget, remoteView);
}
}
widget intent broadcast receiver
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class WidgetIntentReceiver extends BroadcastReceiver {
private static int currentImage = 0;
int[] images = {R.drawable.j_1};
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.appwidget.intent.action.CHANGE_PICTURE"));
RemoteViews remote = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
remote.setImageViewResource(R.id.widget_image_view, getImageToSet());
}
private int getImageToSet(){
currentImage++;
return currentImage = currentImage % images.length ;
}
}
There is a typo in your manifest. This:
<action android:name="android.appwidget.action.APPWIGET_UPDATE"/>
should be changed to this:
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
Notice the missing "D" in "APPWIGET_UPDATE" in the original.

Starting alarm to call IntentService at boot time

I'm trying to set an alarm at boot time, and have it call an IntentService periodically via the AlarmManager. The AlarmReceiver is never triggered, not sure why.
BootReceiver:
package com.company.android;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.text.format.Time;
public class BootBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Time now = new Time();
now.setToNow();
Log.e("#######################COMM", "Booting up " + now.toString());
Intent alarmIntent = new Intent("com.company.android.AlarmReceiver");
PendingIntent pi = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(), 1000 * 5, pi);
Log.e("#######################COMM", "Booted up " + now.toString());
}
}
AlarmReceiver:
package com.company.android;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
/**
* Created by mlaino on 7/8/13.
*/
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("COMM", "Pre");
Intent i = new Intent("com.company.android.PollingService");
context.startService(i);
Log.d("COMM","Pos");
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.company.android" android:versionCode="1" android:versionName="1.0-SNAPSHOT">
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="15" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".BootBroadcastReceiver"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="com.company.android.AlarmReceiver" />
</intent-filter>
</receiver>
<activity android:name=".CommunicationsManagerActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".PollingService" >
<intent-filter>
<action android:name="com.company.android.PollingService" />
</intent-filter>
</service>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</manifest>
Any clues?
Thanks
First, remove android:permission="android.permission.RECEIVE_BOOT_COMPLETED", as you are saying that whatever is calling your receiver must hold that permission, which may or may not be the case.
Then, be sure to run one of your activities before rebooting, as manifest-registered BraodcastReceivers will not work until something explicitly runs one of your components, typically accomplished by the user launching an activity.

Do android widgets require default activity to launch?

I searched a lot for this question and tried everything mentioned on the internet but still couldn't find a solution. My widget gets installed but does not update on clicking. I have not created a default activity for it.
My problem is that my action on clicking the button is not changing.
Here is my code:
//Android manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.pictureapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name=".MyWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget_info" />
</receiver>
<receiver
android:name="MyWidgetIntentReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.example.android.intent.action.CHANGE_PICTURE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget_info" />
</receiver>
</application>
</manifest>
//MyWidgetProvider.java- this class contains the onUpdate method and has listeners.
package com.example.android.pictureapp;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import com.example.android.pictureapp.R;
public class MyWidgetProvider extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds){
//Log.i("Tag","In onUpdate method");
System.out.println("Tag In onUpdate method");
RemoteViews views=new RemoteViews(context.getPackageName(),R.layout.widget_layout);
views.setOnClickPendingIntent(R.id.widget_button, buildButtonPendingIntent(context));
Log.i("Tag","Context value after setOnClickPI before pushWIDGET:"+context);
pushWidgetUpdate(context,views);
Log.i("Tag","after pushwidget update:"+context);
}
public static PendingIntent buildButtonPendingIntent(Context context){
Intent intent=new Intent();
intent.setAction("com.example.android.intent.action.CHANGE_PICTURE");
Log.i("Tag","intent's action:"+intent);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public static void pushWidgetUpdate(Context context,RemoteViews views){
System.out.println("Inside pushwidget");
ComponentName myWidget=new ComponentName(context, MyWidgetProvider.class);
AppWidgetManager manager=AppWidgetManager.getInstance(context);
manager.updateAppWidget(myWidget, views);
}
}
//MywidgetIntentReceiver.java - This class contains onReceive method and performs the function to do after clicking.
package com.example.android.pictureapp;
import com.example.android.pictureapp.R;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
public class MyWidgetIntentReceiver extends BroadcastReceiver {
private static int clickCount = 0;
#Override
public void onReceive(Context context, Intent intent) {
// Log.i("Tag1","In onReceive:"+intent);
System.out.println("In onReceive()");
if (intent.getAction().equals(
"com.example.android.intent.action.CHANGE_PICTURE")) {
updateWidgetPictureAndButtonListener(context);
}
}
private void updateWidgetPictureAndButtonListener(Context context) {
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
views.setImageViewResource(R.id.widget_image, getImageToSet());
System.out.println("in updateWidgetPicture method");
// Log.i("Tag","in updaeWIdgetPicture method");
// remember to set ur button click listeners
views.setOnClickPendingIntent(R.id.widget_button,
MyWidgetProvider.buildButtonPendingIntent(context));
MyWidgetProvider.pushWidgetUpdate(context.getApplicationContext(),
views);
}
private int getImageToSet() {
clickCount++;
Log.i("Tag", "in getImageToSet()" + clickCount);
return clickCount % 2 == 0 ? R.drawable.paypal_logo : R.drawable.paypaldonation;
}
}
//layout/widget_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="5sp"
>
<ImageView
android:id="#+id/widget_image"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_gravity="center"
android:src="#drawable/paypal_logo"
/>
<Button
android:id="#+id/widget_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Change it"
/>
</LinearLayout>
//xml/widget_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dp"
android:minHeight="146dp"
android:updatePeriodMillis="5000"
android:initialLayout="#layout/widget_layout"
>
</appwidget-provider>
//MainACtivity
package com.example.android.pictureapp;
import com.example.android.pictureapp.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
And these are the changes to the manifest:
<activity
android:name="com.example.android.pictureapp.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Starting Android 3.1, your app must have at least one Activity that the user can launch before it will receive any broadcasts etc.
Since you don't have an Activity, your widget's broadcast probably isn't received because the system thinks your app is in a stopped state. Try adding an Activity.

Android battery level widget

I'm working on android widget that displays the level of battery left.
It seem to work on the emulator except that the emulator's battery level doesn't move, so I can't test further. On real device (ICS), it only displays the initial textview("TEST") and doesn't do anything else.
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.battery"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name="main.MyBatteryWidget"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="#xml/widget_info" />
</receiver>
<service android:name="main.MyBatteryWidget$BatteryUpdateService"
android:exported="false" >
<intent-filter>
<action android:name="com.example.battery.action.UPDATE" />
</intent-filter>
</service>
</application>
widget_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:updatePeriodMillis="0"
android:minWidth="40dip"
android:minHeight="72dip"
android:initialLayout="#layout/widget_layout" >
</appwidget-provider>
widget_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="#+id/textView"
android:text="TEST"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
MyBatteryWidget.java
package main;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;
import com.example.battery.R;
public class MyBatteryWidget extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Log.d("TEST", "onUpdate");
context.getApplicationContext().startService(new Intent("com.example.battery.action.UPDATE"));
}
public static class BatteryUpdateService extends Service {
private static int level = 0;
private static int scale = 0;
private BroadcastReceiver receiver = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
Log.d("TEST", "receive");
updateViews(context);
}
}
};
private void updateViews(Context context) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
views.setTextViewText(R.id.textView, String.valueOf(level / scale));
ComponentName componentName = new ComponentName(context, MyBatteryWidget.class);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
appWidgetManager.updateAppWidget(componentName, views);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.d("TEST", "start");
registerReceiver(receiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
return super.onStartCommand(intent, flags, startId);
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
}
Logcat
03-27 19:51:34.963: D/dalvikvm(608): Not late-enabling CheckJNI (already on)
03-27 19:51:35.043: E/Trace(608): error opening trace file: No such file or directory (2)
03-27 19:51:35.083: D/TEST(608): onUpdate
03-27 19:51:35.103: D/TEST(608): start
03-27 19:51:35.133: D/TEST(608): receive
For a working example have a look at my Mini Status Widget, see code
Intent.ACTION_BATTERY_CHANGED
You have to listen it in Android Manifest
<receiver android:name=".myReceiver" >
<intent-filter>
<action android:name="Intent.ACTION_BATTERY_CHANGED" />
</intent-filter>
</receiver>

Categories

Resources