Android battery level widget - android

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>

Related

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.

I don't receive the RECEIVE_BOOT_COMPLETED in the Emulator

I'm French sorry for my English. I'm trying to develop an autoboot service that runs in the background and catches SMS. After a lot of research, I wasn't able to solve my problem.
I can't run the service after the boot and I don't receive the log
Log.v("LTM","MyReceiver.onReceive: "+intent.getAction());
that should to be printed by the BootReceiver. (CF Code below)
I verified the AndroidManifest, tested different code, and nothing works right. I use Eclipse and test the code with the Android Virtual Devices, so maybe the problem comes from the Emulator and I tested with a real device but it didn't start too.
You can find the code below. Thank you for your help.
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.smsmanager"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<service android:enabled="true" android:label="ServiceGUI" android:name="com.smsmanager.MainService"/>
<receiver android:enabled="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED" android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<receiver class="com.smsmanager.SMSReceiver" android:name="com.smsmanager.SMSReceiver">
<intent-filter android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
</manifest>
Main:
package com.smsmanager;
import com.smsmanager.SMSReceiver;
import android.os.IBinder;
import android.util.Log;
import android.app.Service;
import android.content.Intent;
public class MainService extends Service {
SMSReceiver rec;
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.v("LTM", "Demarrage du service");
rec=new SMSReceiver();
Log.v("LTM", "Retour dans le service");
return super.onStartCommand(intent, flags, startId);
}
}
Receiver:
package com.smsmanager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context ctx, Intent intent) {
// TODO Auto-generated method stub
Log.v("LTM","MyReceiver.onReceive: "+intent.getAction());
Intent intent1=new Intent(ctx,MainService.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startService(intent1);
}
}

What kind of technology need I use if the function doWork will run a long time

The function doWork maybe run 20s, I think the system will cause ANR problem.
I don't know what kind of technology I need use to prevent ANR, AyncTask, local server, thread, IntentService or others.
Could you give me some sample codes? Thanks!
AlarmManagerBroadcastReceiver.java
package com.code4reference.enabledisablebroadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {
private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
#Override
public void onReceive(Context context, Intent intent) {
doWork(context,intent);
}
private void doWork(Context context, Intent intent){
//It will running a long time.
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.enabledisablebroadcastreceiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.code4reference.enabledisablebroadcastreceiver.EnableDisableBroadcastReceiver"
android:label="#string/title_activity_enable_disable_boradcast_receiver" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Broadcast receiver -->
<receiver android:name="com.code4reference.enabledisablebroadcastreceiver.AlarmManagerBroadcastReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
</manifest>
EnableDisableBroadcastReceiver.java
package com.code4reference.enabledisablebroadcastreceiver;
import android.app.Activity;
import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.example.enabledisablebroadcastreceiver.R;
public class EnableDisableBroadcastReceiver extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnExit=(Button)findViewById(R.id.btnExit);
btnExit.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
public void enableBroadcastReceiver(View view){
ComponentName receiver = new ComponentName(this, AlarmManagerBroadcastReceiver.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Toast.makeText(this, "Enabled broadcast receiver", Toast.LENGTH_SHORT).show();
}
public void disableBroadcastReceiver(View view){
ComponentName receiver = new ComponentName(this, AlarmManagerBroadcastReceiver.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
Toast.makeText(this, "Disabled broadcst receiver", Toast.LENGTH_SHORT).show();
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="#dimen/padding_medium"
android:text="#string/enable_broadcast_receiver"
android:onClick="enableBroadcastReceiver"
tools:context=".EnableDisableBroadcastReceiver" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="#dimen/padding_medium"
android:text="#string/disable_broadcast_receiver"
android:onClick="disableBroadcastReceiver"
tools:context=".EnableDisableBroadcastReceiver" />
<Button
android:id="#+id/btnExit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Exit"
/>
</LinearLayout>

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 AppwidgetProvider never called

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.

Categories

Resources