Написал для своего устройства версии 2.3 виджет https://play.google.com/store/apps/deta ... prs_widget
Все работает как задумал, но на версиях 4.0 и 4.1 после установки люди не могут его найти.
Вот код самого класса виджета
Код: Выделить всё
package com.rusdelphi.gprs_widget;
import java.lang.reflect.Method;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.widget.RemoteViews;
public class MyWidget extends AppWidgetProvider {
static int mCountUpdate = 0;
static boolean isConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
try {
Class cmClass = Class.forName(cm.getClass().getName());
Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
method.setAccessible(true); // Make the method callable
return (Boolean) method.invoke(cm);
} catch (Exception e) {
}
return false;
}
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int i : appWidgetIds) {
updateWidget(context, appWidgetManager, i);
}
}
@Override
public void onEnabled(Context context) {
mCountUpdate = 0;
super.onEnabled(context);
}
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
}
static void change(Context context, boolean newState) {
try {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
Method method = connectivityManager.getClass().getMethod(
"setMobileDataEnabled", boolean.class);
method.invoke(connectivityManager, newState);
} catch (Exception e) {
e.printStackTrace();
}
}
static void updateWidget(Context ctx, AppWidgetManager appWidgetManager,
int widgetID) {
Intent updateIntent = new Intent(ctx, MyWidget.class);
updateIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
new int[] { widgetID });
PendingIntent pIntent = PendingIntent.getBroadcast(ctx, widgetID,
updateIntent, 0);
RemoteViews widgetView = new RemoteViews(ctx.getPackageName(),
R.layout.widget);
widgetView.setOnClickPendingIntent(R.id.imageView1, pIntent);
if (mCountUpdate == 0) {
if (isConnected(ctx) == true) {
widgetView.setImageViewResource(R.id.imageView1,
R.drawable.gprs_vkl_72);
} else {
widgetView.setImageViewResource(R.id.imageView1,
R.drawable.gprs_vikl_72);
}
} else {
if (isConnected(ctx) == false) {
change(ctx, true);
widgetView.setImageViewResource(R.id.imageView1,
R.drawable.gprs_vkl_72);
} else {
change(ctx, false);
widgetView.setImageViewResource(R.id.imageView1,
R.drawable.gprs_vikl_72);
}
}
// Обновляем виджет
appWidgetManager.updateAppWidget(widgetID, widgetView);
mCountUpdate += 1;
}
}
Код: Выделить всё
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rusdelphi.gprs_widget"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver
android:name="MyWidget"
android:icon="@drawable/gprs_vkl_72"
android:label="@string/widget_name" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" >
</action>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_metadata" />
</receiver>
</application>
</manifest>