- src/ButtonWidget.java
- ==============================
- package com.rved.buttonwidget;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.appwidget.AppWidgetManager;
- import android.appwidget.AppWidgetProvider;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
- import android.view.WindowManager;
- import android.widget.RemoteViews;
- import android.widget.Toast;
- public class ButtonWidget extends AppWidgetProvider {
- //public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
- public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
- @Override
- public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
- Toast.makeText(context, "onUpdate", Toast.LENGTH_SHORT).show();
- /*
- WindowManager.LayoutParams lp = getWindow().getAttributes();
- Toast.makeText(getBaseContext(),"Button is ",Toast.LENGTH_LONG).show();
- lp.screenBrightness = 0.004F;
- float BackLightValue = lp.screenBrightness;
- int SysBackLightValue = (int)(BackLightValue * 255);
- android.provider.Settings.System.putInt(getContentResolver(),android.provider.Settings.System.SCREEN_BRIGHTNESS,SysBackLightValue);
- getWindow().setAttributes(lp);
- */
- RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
- Intent active = new Intent(context, ButtonWidget.class);
- active.setAction(ACTION_WIDGET_RECEIVER);
- active.putExtra("msg", "Message for Button 1");
- PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
- remoteViews.setOnClickPendingIntent(R.id.SwitchButton, actionPendingIntent);
- appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
- }
- @Override
- public void onReceive(Context context, Intent intent) {
- //this takes care of managing the widget
- // v1.5 fix that doesn't call onDelete Action
- final String action = intent.getAction();
- if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
- final int appWidgetId = intent.getExtras().getInt(
- AppWidgetManager.EXTRA_APPWIDGET_ID,
- AppWidgetManager.INVALID_APPWIDGET_ID);
- if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
- this.onDeleted(context, new int[] { appWidgetId });
- }
- } else {
- // check, if our Action was called
- if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
- String msg = "null";
- try {
- msg = intent.getStringExtra("msg");
- } catch (NullPointerException e) {
- Log.e("Error", "msg = null");
- }
- Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
- PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
- NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
- Notification noty = new Notification(R.drawable.icon, "Button 1 clicked", System.currentTimeMillis());
- noty.setLatestEventInfo(context, "Notice", msg, contentIntent);
- notificationManager.notify(1, noty);
- } else {
- // do nothing
- }
- super.onReceive(context, intent);
- }
- }
- }
- res/drawable/toggle_button.xml
- ==============================
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:drawable="@drawable/but_night"
- android:state_pressed="true" />
- <item android:drawable="@drawable/but_day" />
- </selector>
- res/layout/main.xml
- ==============================
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:gravity="center|top"
- android:layout_margin="0dp"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:baselineAligned="false"
- >
- <Button
- android:layout_width="60dip"
- android:layout_height="60dip"
- android:background="@drawable/toggle_button"
- android:id="@+id/SwitchButton"
- android:gravity="center|top"
- />
- <TextView
- android:id="@+id/title"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/day"
- android:textColor="#FFFFFF"
- android:textSize="12dp"
- android:gravity="center|bottom"
- />
- </LinearLayout>
- res/xml/button_widget_provider.xml
- ==============================
- <?xml version="1.0" encoding="utf-8"?>
- <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
- android:minWidth="40dip"
- android:minHeight="40dip"
- android:updatePeriodMillis="0"
- android:initialLayout="@layout/main"
- />
- AndroidManifest.xml
- ==============================
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.rved.buttonwidget"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <!-- Broadcast Receiver that will process AppWidget updates -->
- <receiver android:name=".ButtonWidget" android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
- <!-- Broadcast Receiver that will also process our self created action -->
- <action android:name="de.thesmile.android.widget.buttons.ButtonWidget.ACTION_WIDGET_RECEIVER"/>
- </intent-filter>
- <meta-data android:name="android.appwidget.provider" android:resource="@xml/button_widget_provider" />
- </receiver>
- </application>
- <uses-sdk android:minSdkVersion="3" />
- <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
- </manifest>