1. src/ButtonWidget.java
  2. ==============================
  3. package com.rved.buttonwidget;
  4. import android.app.Notification;
  5. import android.app.NotificationManager;
  6. import android.app.PendingIntent;
  7. import android.appwidget.AppWidgetManager;
  8. import android.appwidget.AppWidgetProvider;
  9. import android.content.Context;
  10. import android.content.Intent;
  11. import android.util.Log;
  12. import android.view.WindowManager;
  13. import android.widget.RemoteViews;
  14. import android.widget.Toast;
  15. public class ButtonWidget extends AppWidgetProvider {
  16. //public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
  17. public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
  18. @Override
  19. public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
  20. Toast.makeText(context, "onUpdate", Toast.LENGTH_SHORT).show();
  21. /*
  22. WindowManager.LayoutParams lp = getWindow().getAttributes();
  23. Toast.makeText(getBaseContext(),"Button is ",Toast.LENGTH_LONG).show();
  24. lp.screenBrightness = 0.004F;
  25. float BackLightValue = lp.screenBrightness;
  26. int SysBackLightValue = (int)(BackLightValue * 255);
  27. android.provider.Settings.System.putInt(getContentResolver(),android.provider.Settings.System.SCREEN_BRIGHTNESS,SysBackLightValue);
  28. getWindow().setAttributes(lp);
  29. */
  30. RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
  31. Intent active = new Intent(context, ButtonWidget.class);
  32. active.setAction(ACTION_WIDGET_RECEIVER);
  33. active.putExtra("msg", "Message for Button 1");
  34. PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
  35. remoteViews.setOnClickPendingIntent(R.id.SwitchButton, actionPendingIntent);
  36. appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
  37. }
  38. @Override
  39. public void onReceive(Context context, Intent intent) {
  40. //this takes care of managing the widget
  41. // v1.5 fix that doesn't call onDelete Action
  42. final String action = intent.getAction();
  43. if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
  44. final int appWidgetId = intent.getExtras().getInt(
  45. AppWidgetManager.EXTRA_APPWIDGET_ID,
  46. AppWidgetManager.INVALID_APPWIDGET_ID);
  47. if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
  48. this.onDeleted(context, new int[] { appWidgetId });
  49. }
  50. } else {
  51. // check, if our Action was called
  52. if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
  53. String msg = "null";
  54. try {
  55. msg = intent.getStringExtra("msg");
  56. } catch (NullPointerException e) {
  57. Log.e("Error", "msg = null");
  58. }
  59. Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
  60. PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
  61. NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
  62. Notification noty = new Notification(R.drawable.icon, "Button 1 clicked", System.currentTimeMillis());
  63. noty.setLatestEventInfo(context, "Notice", msg, contentIntent);
  64. notificationManager.notify(1, noty);
  65. } else {
  66. // do nothing
  67. }
  68. super.onReceive(context, intent);
  69. }
  70. }
  71. }
  72. res/drawable/toggle_button.xml
  73. ==============================
  74. <?xml version="1.0" encoding="utf-8"?>
  75. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  76. <item android:drawable="@drawable/but_night"
  77. android:state_pressed="true" />
  78. <item android:drawable="@drawable/but_day" />
  79. </selector>
  80. res/layout/main.xml
  81. ==============================
  82. <LinearLayout
  83. xmlns:android="http://schemas.android.com/apk/res/android"
  84. android:orientation="vertical"
  85. android:gravity="center|top"
  86. android:layout_margin="0dp"
  87. android:layout_width="fill_parent"
  88. android:layout_height="fill_parent"
  89. android:baselineAligned="false"
  90. >
  91. <Button
  92. android:layout_width="60dip"
  93. android:layout_height="60dip"
  94. android:background="@drawable/toggle_button"
  95. android:id="@+id/SwitchButton"
  96. android:gravity="center|top"
  97. />
  98. <TextView
  99. android:id="@+id/title"
  100. android:layout_width="fill_parent"
  101. android:layout_height="wrap_content"
  102. android:text="@string/day"
  103. android:textColor="#FFFFFF"
  104. android:textSize="12dp"
  105. android:gravity="center|bottom"
  106. />
  107. </LinearLayout>
  108. res/xml/button_widget_provider.xml
  109. ==============================
  110. <?xml version="1.0" encoding="utf-8"?>
  111. <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
  112. android:minWidth="40dip"
  113. android:minHeight="40dip"
  114. android:updatePeriodMillis="0"
  115. android:initialLayout="@layout/main"
  116. />
  117. AndroidManifest.xml
  118. ==============================
  119. <?xml version="1.0" encoding="utf-8"?>
  120. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  121. package="com.rved.buttonwidget"
  122. android:versionCode="1"
  123. android:versionName="1.0">
  124. <application android:icon="@drawable/icon" android:label="@string/app_name">
  125. <!-- Broadcast Receiver that will process AppWidget updates -->
  126. <receiver android:name=".ButtonWidget" android:label="@string/app_name">
  127. <intent-filter>
  128. <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
  129. <!-- Broadcast Receiver that will also process our self created action -->
  130. <action android:name="de.thesmile.android.widget.buttons.ButtonWidget.ACTION_WIDGET_RECEIVER"/>
  131. </intent-filter>
  132. <meta-data android:name="android.appwidget.provider" android:resource="@xml/button_widget_provider" />
  133. </receiver>
  134. </application>
  135. <uses-sdk android:minSdkVersion="3" />
  136. <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
  137. </manifest>