Android AppWidget Config Activity not working on device - android

I have an AppWidget with a configuration activity that launches when the widget is added to the home screen. It works perfectly fine on the emulator, but when I load on a physical device, the widget is not added to the home screen after the configuration activity finishes. Both the emulator and the hardware device are a Samsung Galaxy S7, API level 23, with the app's target API set to 26.
Even if I remove all my logic and just have an empty widget and an empty configuration activity, it does not work on my device. But as soon as I remove the activity from the appwidget-provider xml tag, it works fine.
Any ideas? I'm thinking it must be something with the system settings, maybe the storage options?
I notice these errors that appear when running on the device, but they don't look directly related to the problem
E/MotionRecognitionManager: mSContextService = android.hardware.scontext.ISContextService$Stub$Proxy#e5f8725
E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy#57800fa
E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy#57800fa
Configuration Activity
public class ConfigureActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_configure);
setResult(RESULT_CANCELED);
}
public void onDoneClick(View view) {
setResult(RESULT_OK);
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, WidgetActivity.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 0);
sendBroadcast(intent);
finish();
}
}
Widget Activity
public class WidgetActivity extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_activity);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
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
}
}
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.widget">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:installLocation="internalOnly"
android:theme="#style/AppTheme">
<activity android:name=".ConfigureActivity">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</intent-filter>
</activity>
<receiver android:name=".WidgetActivity">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget_activity_info" />
</receiver>
</application>
</manifest>
widget_activity_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:configure="com.example.widget.ConfigureActivity"
android:initialKeyguardLayout="#layout/widget_activity"
android:initialLayout="#layout/widget_activity"
android:minHeight="110dp"
android:minWidth="250dp"
android:previewImage="#drawable/example_appwidget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen|keyguard">
</appwidget-provider>

Related

android camera led flash light widget not working

I am trying to build and led flash light widget but compiler is not showing any error whenever I try to press widget there show widget loading problem
and android studio does not show any error I am beginner so please help me. Thanks in advance
Meanifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mateen.flash_light_widget">
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera"/>
<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" android:icon="#drawable/example_appwidget_preview" 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/new_app_widget_info" />
</receiver>
<receiver android:name="FlashlightWidgetReceiver">
<intent-filter>
<action android:name="COM_FLASHLIGHT"></action>
</intent-filter>
</receiver>
</application>
</manifest>
appwidgetprovider class
public class NewAppWidget extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Intent receiver = new Intent(context, FlashlightWidgetReceiver.class);
receiver.setAction("COM_FLASHLIGHT");
receiver.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, receiver, 0);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.new_app_widget);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, views);
}
}
recevier there is error I think compiler is also not showing any error
public class FlashlightWidgetReceiver extends BroadcastReceiver {
private static boolean isLightOn = false;
private static Camera camera;
#Override
public void onReceive(Context context, Intent intent) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
if(isLightOn) {
views.setImageViewResource(R.id.button, R.drawable.example_appwidget_preview);
} else {
views.setImageViewResource(R.id.button, R.drawable.example_appwidget_preview);
}
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
appWidgetManager.updateAppWidget(new ComponentName(context, NewAppWidget.class),
views);
if (isLightOn) {
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
isLightOn = false;
}
} else {
// Open the default i.e. the first rear facing camera.
camera = Camera.open();
if(camera == null) {
Toast.makeText(context,"noCamera", Toast.LENGTH_SHORT).show();
} else {
// Set the torch flash mode
Camera.Parameters param = camera.getParameters();
param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
try {
camera.setParameters(param);
camera.startPreview();
isLightOn = true;
} catch (Exception e) {
Toast.makeText(context,"no flash", Toast.LENGTH_SHORT).show();
}
}
}
}
}
layout file for widget
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="#layout/new_app_widget"
android:initialLayout="#layout/new_app_widget"
android:minHeight="40dp"
android:minWidth="40dp"
android:previewImage="#drawable/example_appwidget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen|keyguard"></appwidget-provider>
add these permissions to your manifest
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera.flash" />
i found solution In order to work, the camera needs a surface to turn on.
so after camera.startPreview() i wrote this piece of lines
surfaceTexture = new SurfaceTexture(0);
camera.setPreviewTexture(surfaceTexture);
it solved my problem thanks alot :)

Widget loads as app

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

Widget not updating

I'm trying to create a battery widget, but the textView that is supposed to show battery state isn't updating. It only changes if the phone is restarted. What is missing from the code?
AndroidManifest:
<uses-permission android:name="android.permission.BATTERY_STATS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".WidgetConfigurationActivity"
android:excludeFromRecents="true"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
<receiver android:name=".MAppWidgetProvider" >
<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>
AppWidgetProvider:
public class MAppWidgetProvider extends AppWidgetProvider
{
private String batteryLevel="init";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds)
{
context.getApplicationContext().registerReceiver(this, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
updateView(context);
}
#Override
public void onReceive(Context context, Intent intent)
{
int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int level = -1;
level = (rawlevel * 100) / scale;
batteryLevel = Integer.toString(level);
updateView(context);
super.onReceive(context, intent);
}
public void updateView(Context context)
{
RemoteViews thisViews = new RemoteViews(context.getApplicationContext()
.getPackageName(), R.layout.widget_layout);
thisViews.setTextViewText(R.id.textView1, batteryLevel);
ComponentName thisWidget = new ComponentName(context,
AnalogClockWidgetProvider.class);
AppWidgetManager.getInstance(context).updateAppWidget(thisWidget,
thisViews);
}
}
Info:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="40dp"
android:minHeight="40dp"
android:initialLayout="#layout/widget_layout"
android:updatePeriodMillis="10000"
android:configure="in.wptrafficanalyzer.appwidgetclockconfiguration.WidgetConfigurationActivity"
android:resizeMode="horizontal|vertical">
</appwidget-provider>
First of all you need to use a service to update your widget because activity is in onPause state when you are outside application.
Secondly in service register an intent filter for receiving changes in battery status. And when its getting updated, notify appwidget.
You can also keep a variable in service to which widget will refer and set its level.

Start MainActivity from AppWidget doesn't work

I'm trying to launch my MainActivity from my AppWidget with pending intent but I can't figure out why it doesn't fire the intent. The widget has a ListView with up to 3 items, which is bind to a RemoteViewsService. I want to start my MainActivity when any of the rows is clicked. Can anyone tell me what I might done wrong?
I already checked some related posts like this Launching an activity from an AppWidget, and even tried to run the sample from here https://github.com/commonsguy/cw-advandroid/tree/master/AppWidget, and while the sample works, still can't tell what I did differently.
Here are some of my code that are relevant, please let me know if I should post more, and apologize in advance if my post doesn't look right as this is my first question:
Widget layout:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/stocks"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
AppWidgetProvider class:
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.d(LOGTAG, "onUpdate");
Log.d(LOGTAG, "appWidgetIds.length="+appWidgetIds.length);
for (int i = 0; i < appWidgetIds.length; ++i) {
Log.d(LOGTAG, "appWidgetIds[i]="+appWidgetIds[i]);
}
// update each of the app widgets with the remote adapter
for (int i = 0; i < appWidgetIds.length; ++i) {
updateWidget(context, appWidgetManager, appWidgetIds[i]);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
public void updateWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
Log.d(LOGTAG, "updateWidget id="+appWidgetId);
// Sets up the intent that points to the StackViewService that will
// provide the views for this collection.
Intent intent = new Intent(context, StockAppWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
// When intents are compared, the extras are ignored, so we need to embed the extras
// into the data so that the extras will not be ignored.
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.stock_appwidget);
rv.setRemoteAdapter(R.id.stocks, intent);
Intent contentIntent = new Intent(context, MainActivity.class);
//contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent startAppPendingIntent =
PendingIntent.getActivity(context, 0, contentIntent, 0);
rv.setPendingIntentTemplate(R.id.stocks, startAppPendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, rv);
}
manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="x40241.chris.yuan.a4.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Light.DarkActionBar" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SearchableActivity" >
<intent-filter>
<action android:name="x40241.chris.yuan.a4.app.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<service android:name=".StockServiceImpl" />
<service android:name=".StockAppWidgetService"
android:permission="android.permission.BIND_REMOTEVIEWS" />
<receiver android:name=".ServiceLauncher">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".StockAppWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="#xml/stock_appwidget_info" />
</receiver>
</application>
</manifest>
I think I found the answer, or at least a workable solution in a related post:
AdapterViewFlipper in app widget: setPendingIntentTemplate() and setOnClickFillInIntent() not working
It turns out I wasn't setting the FillInIntent properly. I should call setOnClickFillInIntent on the top level view of the list item instead of the list itself. Here's the code change that made it work in RemoteViewsFactory:
public RemoteViews getViewAt(int position) {
if (DEBUG) Log.d(LOGTAG, "StockRemoteViewsFactory getViewAt position="+position);
RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
rv.setTextViewText(R.id.text_symbol, mStockItems.get(position).getSymbol());
rv.setTextViewText(R.id.text_price, String.format("$%.2f", mStockItems.get(position).getPrice()));
Intent fillInIntent = new Intent(Intent.ACTION_MAIN);
fillInIntent.putExtra(StockAppWidgetProvider.ITEM_NUM, position);
// set on the top level view of the list item, instead of the list view itself
//rv.setOnClickFillInIntent(R.id.stocks, fillInIntent);
rv.setOnClickFillInIntent(R.id.general_layout, fillInIntent);
return rv;
}

How to call widget on the Activity (Widget calling from Activity)

I am new to android widget.I did sample application , that want to show time .How to call widget from activity:
I tried like this but It say Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.weather/com.weather.CurrentCity}; have you declared this activity in your AndroidManifest.xml?
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.weather"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="Weather">
<!-- Broadcast Receiver that will process AppWidget updates -->
<receiver android:name=".CurrentCity" 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/hello_widget_provider" />
</receiver>
<activity android:name=".WeatherActivity"
android:label="Weather Application">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
and my initail activity :
public class WeatherActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent showContent = new Intent(getApplicationContext(),CurrentCity.class);
startActivity(showContent);
}
and my widget activity:
public class CurrentCity extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1,
1000);
}
private class MyTime extends TimerTask {
RemoteViews remoteViews;
AppWidgetManager appWidgetManager;
ComponentName thisWidget;
DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault());
public MyTime(Context context, AppWidgetManager appWidgetManager) {
this.appWidgetManager = appWidgetManager;
remoteViews = new RemoteViews(context.getPackageName(),
R.layout.current_weather);
thisWidget = new ComponentName(context, CurrentCity.class);
}
#Override
public void run() {
remoteViews.setTextViewText(R.id.country,"Time = " + format.format(new Date()));
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}
}
and I have created main.xml & current_weather.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:id="#+id/country"
android:text="TextView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="10dp"
android:textColor="#color/solid_yellow"
></TextView>
</RelativeLayout>
and I created hello_widget_provider.xml.xml in the res/xml/hello_widget_provider.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dip"
android:minHeight="72dip"
android:updatePeriodMillis="10000"
android:initialLayout="#layout/main"
/>
Please help me
Thanks in advance
What you are trying to do is effectively impossible. You do this:
Intent showContent = new Intent(getApplicationContext(),CurrentCity.class);
startActivity(showContent);
You can not invoke a widget this way: a widget does not have an Activity you can show.
You need to follow very closely the tutorial at http://developer.android.com/guide/topics/appwidgets/index.html: there is really no succinct way I can think of to convert your code into a working widget, but the guide has everything you need to know/do to make a working widget.
EDIT: note that your widget must be added to the widget container (probably the home screen) and can not be added programmatically. The user of the Android device must do it manually.
I tried to do this in many different ways. But, in Android there is no intent or similar alternative, which can invoke a widget via activity (I mean application). You have to drag the widget and drop it on your home screen.
But, yes, other ways it is possible!

Categories

Resources