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();
}
}
Related
In AndroidStudio i create a new android project an create a app widget and choose option for configuration activity. AndroidStudio generates now the provider-info xml, the declerations in manifest xml an the both java classes. One activity, one widget provider.
This should be runnable but i get error: Could not identify launch activity: Default Activity not found. Error while Launching activity. The launch field also shows a red cross.
I dont understand why because there is no default activity. The configuration activity should start when the widget provider start working. To do so there is a intent-filter for the activitiy with android.appwidget.action.APPWIDGET_CONFIGURE.
I add also categroy in intent-filter LAUNCHER and DEFAULT. In provider and in activity. But still get the error message.
If I choose "Nothing" in launch configuration and I run the app it brings only many error messages: Waiting for application to come online: com.example.desktop_win10.myapplication | com.example.desktop_win10.myapplication.test
But the widget isnt installed and doesnt run. What doing Im wrong? I try out Intellij and AndroidStudio.
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.desktop_win10.myapplication" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<receiver android:name=".NewAppWidget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/new_app_widget_info" />
</receiver>
<activity android:name=".NewAppWidgetConfigureActivity" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
</application>
</manifest>
widget_info.xml:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="40dp"
android:minHeight="40dp"
android:updatePeriodMillis="86400000"
android:previewImage="#drawable/example_appwidget_preview"
android:initialLayout="#layout/new_app_widget"
android:configure="com.example.desktop_win10.myapplication.NewAppWidgetConfigureActivity"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen"
android:initialKeyguardLayout="#layout/new_app_widget">
</appwidget-provider>
And there are the two Java classes.
EDIT1:
Now I add category LAUNCHER and action MAIN:
<activity android:name=".NewAppWidgetConfigureActivity" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
If I start the debugger sometimes the widget ist in the widget store and sometimes not.
I also see that in the generated java activity class is a finish method called:
// If this activity was started with an intent without an app widget ID, finish with an error.
if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
return;
}
But it also doesnt work if I delete thid. I dont unterstand why a the default google example not work.
Here are the java classes:
Activity:
package com.example.desktop_win10.myapplication;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
/**
* The configuration screen for the {#link NewAppWidget NewAppWidget} AppWidget.
*/
public class NewAppWidgetConfigureActivity extends Activity {
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
EditText mAppWidgetText;
private static final String PREFS_NAME = "com.example.desktop_win10.myapplication.NewAppWidget";
private static final String PREF_PREFIX_KEY = "appwidget_";
public NewAppWidgetConfigureActivity() {
super();
}
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// Set the result to CANCELED. This will cause the widget host to cancel
// out of the widget placement if the user presses the back button.
setResult(RESULT_CANCELED);
setContentView(R.layout.new_app_widget_configure);
mAppWidgetText = (EditText)findViewById(R.id.appwidget_text);
findViewById(R.id.add_button).setOnClickListener(mOnClickListener);
// Find the widget id from the intent.
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
// If this activity was started with an intent without an app widget ID, finish with an error.
if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
return;
}
mAppWidgetText.setText(loadTitlePref(NewAppWidgetConfigureActivity.this, mAppWidgetId));
}
View.OnClickListener mOnClickListener = new View.OnClickListener() {
public void onClick(View v) {
final Context context = NewAppWidgetConfigureActivity.this;
// When the button is clicked, store the string locally
String widgetText = mAppWidgetText.getText().toString();
saveTitlePref(context,mAppWidgetId,widgetText);
// It is the responsibility of the configuration activity to update the app widget
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
NewAppWidget.updateAppWidget(context, appWidgetManager, mAppWidgetId);
// Make sure we pass back the original appWidgetId
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
};
// Write the prefix to the SharedPreferences object for this widget
static void saveTitlePref(Context context, int appWidgetId, String text) {
SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
prefs.putString(PREF_PREFIX_KEY + appWidgetId, text);
prefs.apply();
}
// Read the prefix from the SharedPreferences object for this widget.
// If there is no preference saved, get the default from a resource
static String loadTitlePref(Context context, int appWidgetId) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
String titleValue = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null);
if (titleValue != null) {
return titleValue;
} else {
return context.getString(R.string.appwidget_text);
}
}
static void deleteTitlePref(Context context, int appWidgetId) {
SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
prefs.remove(PREF_PREFIX_KEY + appWidgetId);
prefs.apply();
}
}
And Provider:
package com.example.desktop_win10.myapplication;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.widget.RemoteViews;
/**
* Implementation of App Widget functionality.
* App Widget Configuration implemented in {#link NewAppWidgetConfigureActivity NewAppWidgetConfigureActivity}
*/
public class NewAppWidget extends AppWidgetProvider {
#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 onDeleted(Context context, int[] appWidgetIds) {
// When the user deletes the widget, delete the preference associated with it.
for (int appWidgetId : appWidgetIds) {
NewAppWidgetConfigureActivity.deleteTitlePref(context, 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
}
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
CharSequence widgetText = NewAppWidgetConfigureActivity.loadTitlePref(context, appWidgetId);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
views.setTextViewText(R.id.appwidget_text, widgetText);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
Did you add:
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
?
As I know, without it android won't know which activity to launch as the main activity.
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.
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 want to open my application immediately when S Pen is detached , How can you do this, if the methods put under onSPenDetached is only called when my application is opened again?
Thanks,
Chandu
The following works on my Galaxy Tab A 9.7 with S-Pen (SM-P550) running Android 5.0.2.
Attaching and detaching the stylus creates Broadcast Intents of type com.samsung.pen.INSERT with a booleanExtra named penInsert of false if detached and true if put back into the device.
Thus a Broadcast Receiver can be created that filters this kind of events. The following code is for such a Broadcast Receiver which starts OneNote if the stylus is detached:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class SPenDetachIntentBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent penInsertIntent) {
if (!penInsertIntent.getBooleanExtra("penInsert", true)) {
try {
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage("com.microsoft.office.onenote");
context.startActivity(launchIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
In the Manifest file you need to declare it as a receiver listening for com.samsung.pen.INSERT Broadcast Intents with an intent filter. The following entry in a project's AndroidManifest.xml declares SPenDetachBroadcastReceiver, generates an instance and makes it listening for com.samsung.pen.Insert Broadcast Intents:
<receiver
android:name=".SPenDetachIntentBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.samsung.pen.INSERT" />
</intent-filter>
</receiver>
The advantage over using registerSPenDetachmentListener on an SPenEventLibrary object to register a Service with an onSPenDetached method implemented is that you do not need any additional library files and you also do not need additional permissions.
You will need to create a BroadcastReceiver and a Service.
The service:
public class SPenService extends Service {
SPenEventLibrary mSPenEventLibrary = new SPenEventLibrary();
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
mSPenEventLibrary.registerSPenDetachmentListener(this, new SPenDetachmentListener() {
#Override
public void onSPenDetached(boolean bDetached) {
if (bDetached) {
Toast.makeText(SPenService.this, "S Pen Detached", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(SPenService.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} else {
Toast.makeText(SPenService.this, "S Pen Inserted", Toast.LENGTH_SHORT).show();
}
}
});
}
#Override
public void onDestroy() {
super.onDestroy();
mSPenEventLibrary.unregisterSPenDetachmentListener(this);
}
}
The receiver:
public class SPenReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
context.startService(new Intent(context, SPenService.class));
}
}
}
The manifest (inside the <application> tag):
<receiver android:name=".SPenReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".SPenService" >
</service>
After device reboot I receive first APPWIDGET_ENABLED and then twice APPWIDGET_UPDATE.
I spent quite some hours googling this without result.
Is anybody experiencing the same? Have you found a way to avoid calling the update twice?
Here's some code:
<receiver android:name=".Widget" 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/button_widget_provider" />
</receiver>
public void onReceive(final Context context, final Intent intent) {
super.onReceive(context, intent);
final String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action) ) {
Log.i(TAG, "update");
} else if (AppWidgetManager.ACTION_APPWIDGET_ENABLED.equals(action) ) {
Log.i(TAG, "enabled");
}
}
Have you found a way to avoid calling the update twice?
You have no control over how many times you are updated. That is up to the home screen and the app widget framework.
import java.util.ArrayList;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
public class CopyOfWidgetProvider extends AppWidgetProvider {
private static ArrayList<Integer> widgets = new ArrayList<Integer>();
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] widgetIDs) {
super.onUpdate(context, appWidgetManager, widgetIDs);
for (int widgetID : widgetIDs) {
if (!widgets.contains(widgetID)) {
widgets.add(widgetID);
// this code will run only ONCE after reboot
// loop is necessary in cases where there were more than one
// instances of widget before reboot
}
}
}
}