본문 바로가기
프로그래밍 놀이터/안드로이드, Java

[Android/안드로이드] AppWidget Sample Code ( Example ).

by 돼지왕 왕돼지 2012. 2. 15.
반응형


안녕하세요 돼지왕 왕돼지입니다.
AppWidget 을 빠르게 만들 수 있게, 그리고 쉽게 학습할 수 있도록 Sample Code 올립니다.
Test Widget 은 View 로 Button 하나를 가지는 위젯입니다. 
 
@manifest 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.test"
      android:versionCode="1"
      android:versionName="1.0">
   
   <application android:icon="@drawable/icon" android:label="@string/app_name">
 
      <receiver android:name=".ButtonWidget" android:label="Button_widget">
         <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>
  
       <activity android:name=".ClickOneActivity"/>
        
    </application>
</manifest>

 




@AppWidgetProvider 

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) {
       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");
  
       Intent configIntent = new Intent(context, ClickOneActivity.class);
       configIntent.setAction(ACTION_WIDGET_CONFIGURE);
  
       PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
       PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
  
       remoteViews.setOnClickPendingIntent(R.id.button_one, actionPendingIntent);
       remoteViews.setOnClickPendingIntent(R.id.button_two, configPendingIntent);
  
       appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
   }
 
   @Override
   public void onReceive(Context context, Intent intent) {
      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 {
         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);
     }
     super.onReceive(context, intent);
  }
 }
}



@Activity

public class ClickOneActivity extends Activity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.configure);
      Toast.makeText(getApplicationContext(), "We are in ClickOneActivity", Toast.LENGTH_SHORT).show();
   }
}



로그인 없이 추천 가능합니다. 손가락 꾸욱~


반응형

댓글