There are a million and five questions on SO about this, but I've tried sorting through them all, and can not get it working. I know that AppWidgetProvider just extends BroadcastReceiver so I think I can just do everything within my AppWidgetProvider. I've read that the intent filter ACTION_HEADSET_PLUG is Intent.FLAG_RECEIVER_REGISTERED_ONLY and tried following this: Detecting whether a headset is plugged into an Android device or not. to set my filter in code, but it was pretty mish-mash and I couldn't wrap my head around it.
I've toasted out intent.getAction() in my onReceive method and when I plug or unplug my headphones, nothing gets shown. When I press buttons though, I see the int constant associated with them. I'm assuming this is because it's being blocked by the registered only thing.
I also read about some sticky-something-or-other and forked around with that for a while, all to no avail.
here is my AppWidgetManager:
package com.example.musicplayerforburrito;
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.media.MediaPlayer;
import android.widget.RemoteViews;
import android.widget.Toast;
public class MyWidgetProvider extends AppWidgetProvider {
public static String WIDGET_PLAY_BUTTON = "android.appwidget.action.PLAY_PAUSE_WIDGETS";
public static String NEXT_BUTTON = "android.appwidget.action.NEXT_WIDGETS";
public static String PREVIOUS_BUTTON = "android.appwidget.action.PREVIOUS_WIDGETS";
public static String RELOAD_BUTTON = "android.appwidget.action.RELOAD_WIDGETS";
static MusicPlayerClass mpc;
static String CurrentlyPlayingSong = "";
static Context cont;
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
mpc = new MusicPlayerClass(context);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
ComponentName watchWidget = new ComponentName(context, MyWidgetProvider.class);
remoteViews.setOnClickPendingIntent(R.id.playpausewidget, getPendingSelfIntent(context, WIDGET_PLAY_BUTTON));
remoteViews.setOnClickPendingIntent(R.id.forwardwidget, getPendingSelfIntent(context, NEXT_BUTTON));
remoteViews.setOnClickPendingIntent(R.id.backwidget, getPendingSelfIntent(context, PREVIOUS_BUTTON));
remoteViews.setOnClickPendingIntent(R.id.reloadwidget, getPendingSelfIntent(context, RELOAD_BUTTON));
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
//if(intent.hasExtra("state"))
// Toast.makeText(context, "hi jimmy", 2000).show();
Toast.makeText(context, intent.getAction(), 2000).show();
try
{
String b = mpc.currentlyPlayingSong;
}
catch(Exception e)
{
mpc = new MusicPlayerClass(context);
}
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new RemoteViews( context.getPackageName(), R.layout.widget_layout );
ComponentName watchWidget = new ComponentName(context, MyWidgetProvider.class);
cont = context;
if (WIDGET_PLAY_BUTTON.equals(intent.getAction())) {
if(!mpc.hasFolders)
{
mpc.Initialize();
mpc.mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
public void onCompletion(MediaPlayer mp)
{
mpc.songIndex++;
CurrentlyPlayingSong = mpc.StartSong();
UpdateTitle(cont);
}
});
}
if(mpc.isPlaying)
{
mpc.isPlaying = false;
mpc.PauseMusic();
remoteViews.setImageViewResource(R.id.playpausewidget, R.drawable.play);
}
else if(mpc.currentPosition != 0)
{
mpc.isPlaying = true;
mpc.ResumeMusic();
remoteViews.setImageViewResource(R.id.playpausewidget, R.drawable.pause);
}
else
{
remoteViews.setTextViewText(R.id.mp3filename, mpc.StartSong());
remoteViews.setImageViewResource(R.id.playpausewidget, R.drawable.pause);
}
}
else if (NEXT_BUTTON.equals(intent.getAction())) {
mpc.songIndex++;
remoteViews.setTextViewText(R.id.mp3filename, mpc.StartSong());
}
else if (PREVIOUS_BUTTON.equals(intent.getAction())) {
mpc.songIndex--;
remoteViews.setTextViewText(R.id.mp3filename, mpc.StartSong());
}
else if (RELOAD_BUTTON.equals(intent.getAction())) {
mpc.Reload();
mpc.mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
public void onCompletion(MediaPlayer mp)
{
mpc.songIndex++;
CurrentlyPlayingSong = mpc.StartSong();
UpdateTitle(cont);
}
});
remoteViews.setTextViewText(R.id.mp3filename, mpc.StartSong());
remoteViews.setImageViewResource(R.id.playpausewidget, R.drawable.pause);
}
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
private void UpdateTitle(Context context)
{
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new RemoteViews( context.getPackageName(), R.layout.widget_layout );
ComponentName watchWidget = new ComponentName(context, MyWidgetProvider.class);
remoteViews.setTextViewText(R.id.mp3filename, CurrentlyPlayingSong);
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
protected PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, getClass());
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
}
here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.musicplayerforburrito"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<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" />
<action android:name="android.appwidget.action.PLAY_PAUSE_WIDGETS" />
<action android:name="android.appwidget.action.NEXT_WIDGETS" />
<action android:name="android.appwidget.action.PREVIOUS_WIDGETS" />
<action android:name="android.appwidget.action.RELOAD_WIDGETS" />
<action android:name="android.appwidget.action.ACTION_HEADSET_PLUG" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.ACTION_HEADSET_PLUG"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget_info" />
</receiver>
<activity
android:name="com.example.musicplayerforburrito.MusicPlayer"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I've been fighting with this for literally hours which seems really stupid because it's not even that big of a "feature" to pause the music when the headphones are unplugged. Any guidance you can share would be greatly appreciated.
TIA
The answer at https://stackoverflow.com/a/9682818/1682419 quotes Dianne Hackborn (who's in the core Android team). You'll need a Service or Activity that stays running as long as you're interested in these broadcast messages, and it needs to call registerReceiver() to register for these messages.
Note 1: When you invent Intent actions, you're supposed to use your own package name, not android.intent.action, to avoid name conflicts.
Note 2: The widget provider's <intent-filter> should only list the public action android.appwidget.action.APPWIDGET_UPDATE. There's no benefit in including the private on-click actions and android.intent.action.ACTION_HEADSET_PLUG.
Related
my Application widget Never go to the onUpdate function
i add 3000 mili for testings but except the first time you add it,
it never goes to the onUpdate function.
what am i doing wrong ?
my manifest file:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="layout.ClockWidgetClass">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/clock_widget_class_info" />
</receiver>
</application>
my clockWidget:
/**
* Implementation of App Widget functionality.
*/
public class ClockWidgetClass extends AppWidgetProvider {
static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager,
final int appWidgetId) {
//getting shered prefrences
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String name = preferences.getString("randomString", "");
if(!name.equalsIgnoreCase(""))
{
name = name; /* Edit the value here*/
}
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.clock_widget_class);
//set chosen design visible
//set all visibility to 0
views.setViewVisibility(R.id.AnalogClock0, View.INVISIBLE);
views.setViewVisibility(R.id.AnalogClock1, View.INVISIBLE);
views.setViewVisibility(R.id.AnalogClock2, View.INVISIBLE);
views.setViewVisibility(R.id.AnalogClock3, View.INVISIBLE);
//turning on the correct clock
if (name.equals("1")) {
views.setViewVisibility(R.id.AnalogClock0, View.VISIBLE);
} else if (name.equals("2")) {
views.setViewVisibility(R.id.AnalogClock1, View.VISIBLE);
} else if (name.equals("3")) {
views.setViewVisibility(R.id.AnalogClock2, View.VISIBLE);
} else {
views.setViewVisibility(R.id.AnalogClock3, View.VISIBLE);
}
// views.setTextViewText(R.id.appwidget_text, name);
//updateting the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
#Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
#Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
#Override
public void onReceive(Context context, Intent intent) {
// Chain up to the super class so the onEnabled, etc callbacks get dispatched
super.onReceive(context, intent);
// Handle a different Intent
Log.d("reciving", "onReceive()" + intent.getAction());
}
}
my Widget xml file:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="#layout/clock_widget_class"
android:initialLayout="#layout/clock_widget_class"
android:minHeight="110dp"
android:minWidth="250dp"
android:previewImage="#drawable/example_appwidget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="3000"
android:widgetCategory="home_screen"></appwidget-provider>
Never might not be correct. It just doesn't happen in the time you expect it to happen. With the documentation for updatePeriodMillis you'll find an important note:
Note: Updates requested with updatePeriodMillis will not be delivered more than once every 30 minutes.
So you might have to wait for this 30 minutes before another call to onUpdate() happens.
The question is simple as it seems.
The Android Widget works as a charm, everything is OK.
I just want the widget content to be updated when (in the right moment) the user turn on its device screen.
I can't find a hint for this in internet nor the documentation. This means that I'm clearly overlooking something easy and important.
UPDATE
Thanks for the answer of Murtaza, seems perfect, but for some reason isn't working.
My widget has already a receiver, so i added the suggested intent filter:
<receiver android:name=".MyWidget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
</intent-filter>
Inside the widget class i overriden the suggested function:
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
playNotification(context);
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
playNotification(context);
}
}
The playNotification() plays an alarm. It works properly inside the widget. But when I switch the screen ON and OFF, nothing happens.
UPDATE II - A working widget in Android
I post the whole code of the widget for who need it.
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.widget.RemoteViews;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.Calendar;
/**
* Implementation of App Widget functionality.
*/
public class MyWidget extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
updateAppWidget(context, appWidgetManager, appWidgetIds[i]);
}
}
#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) {
String widgetText = "";
playNotification(context, true);
widgetText += " DATE:" + Calendar.getInstance().getTime().getHours() + ":" + Calendar.getInstance().getTime().getMinutes();
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.mywidget);
views.setTextViewText(R.id.appwidget_text, widgetText);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
public static void playNotification(Context context, boolean alarm) {
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (alarm) notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
Ringtone r = RingtoneManager.getRingtone(context, notification);
if (!r.isPlaying())
r.play();
} catch (Exception e) {
e.printStackTrace();
}
}
ANDROID MANIFEST
This is what you need to add to the manifest to have a working widget on Android. More, the filters I would like to make work.
<receiver android:name=".MYWidget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/mywidget_info" />
</receiver>
PLEASE NOTE THAT the code works. Everything except what concern my question: "Update an Android Widget when the screen is turned ON"
You need to create a Broadcast Receiver for that.
Add these permissions in your Manifest.xml
<receiver android:name=".MyBroadcastReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_SCREEN_ON"></action>
<action android:name="android.intent.action.ACTION_SCREEN_OFF"></action>
</intent-filter>
</receiver>
MyBroadcastReceiver.java
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
//update your widget.
}
}
}
The question turns into a new one, so I want to show some example. Your question now is, how to update the widget from inside all other classes. As I suspect, Your playNotification(); method is inside the receiver of Your widget. What You have to do is, make an extra class with Your playNotification() method as public , so You can call it from every other class. Look this example, based on the answer from Murtaza and Your update:
create an extra class:
public class NotificationHelper{
private Context mContext;
//make a constructor and set Context or other objects/values if You need it
public NotificationHelper(Context ctx){
mContext = ctx;
}
public void playNotification(){
//do Your stuff here inside
}
}
With this public class, You can do Your stuff from anywhere else. For example inside Your AppWidget Receiver or Your BroadCastReceiver that will be fired if the screen goes on or off:
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
NotificationHelper mHelper = new NotificationHelper(context);
mHelper.playNotification();
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
NotificationHelper mHelper = new NotificationHelper(context);
mHelper.playNotification();
}
}
Everyone. I'm using "WidgetQuotes Sample" to display some quotes on homescreen for android phone (Android 2.3). But the widget doesn't appear or show up. I don't know why.
Take a look at these code:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.codeskraps.quotes"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".WidgetQuotes" 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_quotes_info" />
</receiver>
<service android:name=".UpdateWidgetService"></service>
<activity
android:name="com.codeskraps.quotes.Main"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="com.codeskraps.quotes.ACTION_WIDGET_CONFIGURE"/>
</intent-filter>
</activity>
</application>
</manifest>
WidgetQuotes.Java
package com.codeskraps.quotes;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class WidgetQuotes extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
// Build the intent to call the service
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
// To react to a click we have to use a pending intent as the
// onClickListener is
// excecuted by the homescreen application
PendingIntent pendingIntent = PendingIntent.getService(
context.getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.widget_textview, pendingIntent);
// Finally update all widgets with the information about the click
// listener
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
// Update the widgets via the service
context.startService(intent);
}
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
}
}
UpdateWidgetService.Java
package com.codeskraps.quotes;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;
public class UpdateWidgetService extends Service {
private static final String TAG = UpdateWidgetService.class.getSimpleName();
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.d(TAG, "onStart started");
// Create some random data
Random random = new Random();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
int[] appWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
if (appWidgetIds.length > 0) {
for (int widgetId : appWidgetIds) {
List<String> qList = getQuotes();
int nextInt = random.nextInt(qList.size());
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget);
remoteViews.setTextViewText(R.id.widget_textview, qList.get(nextInt));
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
stopSelf();
}
super.onStart(intent, startId);
}
public List<String> getQuotes(){
List<String> qList = new ArrayList<String>();
qList.add("When Life Gives You Questions, Google has Answers");
qList.add("1f u c4n r34d th1s u r34lly n33d t0 g37 l41d ");
qList.add("Microsoft: \"You've got questions. We've got dancing paperclips.\"");
qList.add("If at first you don't succeed; call it version 1.0 ");
qList.add("There are 10 types of people in the world: those who understand binary, and those who don't.");
qList.add("I'm not anti-social; I'm just not user friendly");
qList.add("The glass is neither half-full nor half-empty: it's twice as big as it needs to be.");
qList.add("I would love to change the world, but they won't give me the source code");
qList.add("A Life? Cool! Where can I download one of those?");
qList.add("Artificial Intelligence is no match for Natural Stupidity.");
qList.add("Windows has detected you do not have a keyboard. Press 'F9\" to continue.");
qList.add("In a world without fences and walls, who needs Gates and Windows?");
qList.add("MICROSOFT = Most Intelligent Customers Realize Our Software Only Fools Teenagers");
qList.add("\"Concept: On the keyboard of life, always keep one finger on the escape button.\"");
qList.add("My software never has bugs. It just develops random features.");
qList.add("The box said 'Requires Windows 95 or better'. So I installed LINUX.");
qList.add("Never make fun of the geeks, one day they will be your boss.");
qList.add("Girls are like internet domain names, the ones I like are already taken.");
qList.add("Better to be a geek than an idiot.");
qList.add("Failure is not an option -- it comes bundled with Windows.");
return qList;
}
}
widget_quotes_info.xml
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="72dp"
android:updatePeriodMillis="180000"
android:initialLayout="#layout/widget" />
Main.Java
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
sendBroadcast(new Intent(Intent.ACTION_MAIN)
.addCategory(Intent.CATEGORY_HOME));
setContentView(R.layout.main);
}
}
Try restart device(emulator).
or clear cache of Home Screen.
I was similar problem on HTC (Android 2.3.7).
Solved by clear cache of HTC Sense(standard Android Home Screen replacement by HTC...)
The app can't be installed to the sdcard, go into your device's settings->manage applications->find the app then click the 'move to phone' button (assuming that it was actually on the sdcard)..
I have few widgets in my application with different sizes. And for each widget I need set receiver android:name to different values (otherwise Android show only one first widget). So I need to create separate class with identical code for each widget. That is uneasily. How I can avoid this? May be I can create one parent class and inherit it for other widget classes?
My code:
Manifest:
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget_1x1" />
</receiver>
<receiver android:exported="false"
android:name=".WidgetHandler_4x4"
android:label="Widget 4x4" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget_4x4" />
</receiver>
And WidgetHandler_1x1 / WidgetHandler_4x4:
public class WidgetHandler_4x4 extends AppWidgetProvider {
public static String ACTION_WIDGET_EDIT = "ActionWidgetEdit";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent editIntent = new Intent(context, this.getClass());
editIntent.setAction(ACTION_WIDGET_EDIT);
PendingIntent editPendingIntent = PendingIntent.getBroadcast(context, 0, editIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.edit_button, editPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_WIDGET_EDIT)) {
Toast.makeText(context, "edit", Toast.LENGTH_SHORT).show();
}
super.onReceive(context, intent);
}
}
In my application, I have a Fragment which is attached to the main activity. This Fragment displays data with the help of an adapter. In the adapter, I have inflated a layout which has a clickable text "Watch From Home". The adapter displays data from the server. I want to show the same data on the home screen and when I click on the "Watch From Home" text, a home screen widget should be created without any user interaction.
I made a receiver MyWidgetProvider.What should the method onClickText() for "Watch From Home" have so that it takes me to the widget? I must admit, I am new to android. Thanks
Android Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pack.android.receiver"
android:versionCode="1"
android:versionName="1.0" >
<application
android:icon="#drawable/icon"
android:label="#string/app_name" >
<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>
<uses-sdk android:minSdkVersion="8" />
</manifest>
MyWidgetProvider.Java file
public class MyWidgetProvider extends AppWidgetProvider {
private TextView team1Name;
private TextView team2Name;
private TextView team1Score;
private TextView team2Score;
public static boolean widgetView=false;
private static final String LOG = "com.playup.android.receiver";
public MyWidgetProvider(TextView team1Name, TextView team2Name, TextView team1Score, TextView team2Score){
this.team1Name=team1Name;
this.team2Name=team2Name;
this.team1Score=team1Score;
this.team2Score=team2Score;
initializeViews();
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
Log.w(LOG, "onUpdate method called");
// Get all ids
ComponentName thisWidget = new ComponentName(context,MyWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
// Build the intent to call the service
Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
// Update the widgets via the service
context.startService(intent);
}
public void initializeViews(){
// team1Name= (TextView)content_layout.findViewById(R.id.team1Name);
}
}
UpdateWidgetService
public class UpdateWidgetService extends Service {
private static final String LOG = "com.playup.android.receiver";
#Override
public void onStart(Intent intent, int startId) {
Log.i(LOG, "Called");
// Create some random data
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this
.getApplicationContext());
int[] allWidgetIds = intent
.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
ComponentName thisWidget = new ComponentName(getApplicationContext(),
MyWidgetProvider.class);
int[] allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);
Log.w(LOG, "From Intent" + String.valueOf(allWidgetIds.length));
Log.w(LOG, "Direct" + String.valueOf(allWidgetIds2.length));
for (int widgetId : allWidgetIds) {
// Create some random data
int number = (new Random().nextInt(100));
RemoteViews remoteViews = new RemoteViews(this
.getApplicationContext().getPackageName(),0x7f030061); //Since R could not be resolve, I used the generated ids
Log.w("Widget", String.valueOf(number));
// Set the text
remoteViews.setTextViewText(0x7f0a022a,"Random: " + String.valueOf(number));
// Register an onClickListener
Intent clickIntent = new Intent(this.getApplicationContext(),MyWidgetProvider.class);
clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
allWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, clickIntent,PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(0x7f0a022a, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
stopSelf();
super.onStart(intent, startId);
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
Simply saying, You can't create a home widget without any user interaction.
Update:
Your receiver tag should start like this:
<receiver
android:name=".MyWidgetProvider"
android:label="My widget label" >
<!-- The rest as is -->
</receiver>
Home Screen Widget can only add to Home Screen directly by user.Only user can add widget as guest to Home Screen or other applications.
Edit:
To see your widget in widgets list:
It is necessary that register your AppWidgetProvider in manifest.
Your App has to have Launcher/Main Activity,and App must be directly run by user,before it be visible in widgets list.This is necessary ,because no broadcastreceiver(and so no AppWidgetProvider),no service,... of your App could not register before your App run directly by user.If you look at your manifest,you will see that your App has no Launcher/Main Activity(it has only a receiver),so it can not run by user and your AppWidgetProvider(that is a broadcastreceiver) will not register and thereupon you can not see your widget in widget list.
This had me scratching my head for a while.
I had the meta-data tag as child of the intent-filter tag, which is wrong. The meta-data is to be child of the receiver tag.
Incorrect version
<receiver
android:name=".AppWidgetReceiver"
android:label="AppWidgetReceiver" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/appwidget_info" />
</intent-filter>
</receiver>
Following is the correct version
<receiver android:name=".AppWidgetReceiver"
android:label="AppWidgetReceiver" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/appwidget_info" />
</receiver>