2014-07-01 2 views
0

내 앱에서 활동해야합니다. 하나의 활동이 카운트 값을 증가시키는 데 사용되며 다른 활동은 AppWidget을 확장합니다. 카운트 값 (정수)을 앱 위젯에 정기적으로 표시해야합니다.활동에서 위젯까지 하나의 정수를 어떻게 표시합니까?

다음은 나의 활동 코드입니다.

Smoke_Count.java

package com.example.smokecount; 

import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 
import android.view.Menu; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class Smoke_Count extends Activity implements OnTouchListener{ 


    private Button btn; 
    private TextView txt; 
    private static int count=0; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_smoke__count); 

     btn=(Button)findViewById(R.id.button1); 
     txt=(TextView)findViewById(R.id.textView1); 
     txt.setText(String.valueOf(count)); 
     btn.setOnTouchListener(this); 
     loadInt(); 


    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.smoke__count, menu); 
     return true; 
    } 


    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     // TODO Auto-generated method stub 

     if(event.getAction()==MotionEvent.ACTION_UP) 
     { 
      loadInt(); 
      saveInt("countValue", count); 
      count++; 
      txt.setText(String.valueOf(count)); 
     } 
     return true; 
    } 

    public void saveInt(String key,int count) 
    { 
     SharedPreferences share= PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
     Editor edit= share.edit(); 
     edit.putInt(key, count); 
     edit.commit(); 

    } 

    public void loadInt() 
    { 
     SharedPreferences share= PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
     int counter=share.getInt("countValue", count); 
     /*Intent i=new Intent(Smoke_Count.this, Widget.class); 
     i.putExtra("count", counter); 
     sendBroadcast(i);*/ 
     txt.setText(String.valueOf(counter)); 

     if(counter>=count) 
     { 
      count=counter; 
     } 


    } 

} 

그리고이 Widget.java

내 위젯 활동

입니다

package com.example.smokecount; 

import android.app.PendingIntent; 
import android.appwidget.AppWidgetManager; 
import android.appwidget.AppWidgetProvider; 
import android.content.Context; 
import android.content.Intent; 
import android.widget.RemoteViews; 

public class Widget extends AppWidgetProvider{ 



     @Override 
     public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
       int[] appWidgetIds) { 
      // TODO Auto-generated method stub 
      super.onUpdate(context, appWidgetManager, appWidgetIds); 



      for(int i=0;i<appWidgetIds.length;i++) 
      { 
       int appWidgetID=appWidgetIds[i]; 

       Intent i1=new Intent(Intent.ACTION_VIEW, null, context, Smoke_Count.class); 
       PendingIntent pending=PendingIntent.getActivity(context, 0, i1, 0); 

       RemoteViews views=new RemoteViews(context.getPackageName(), R.layout.activity_widget); 
       views.setOnClickPendingIntent(R.id.imageButton1, pending); 
       appWidgetManager.updateAppWidget(appWidgetID, views); 
      } 
     } 

     /*@Override 
     public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 
      super.onReceive(context, intent); 
      String action=intent.getAction(); 
     Bundle ext=intent.getExtras(); 
     int scount=ext.getInt("count"); 
     Toast.makeText(context, ""+scount, Toast.LENGTH_LONG).show(); 
     }*/ 

} 

이 내 위젯 XML 파일입니다

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <ImageButton 
     android:id="@+id/imageButton1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:id="@+id/txtcount" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/imageButton1" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="18dp" 
     android:text="Medium Text" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

</RelativeLayout> 

내가 필요 coun를 표시하다 t 값을 위젯에 입력하십시오.

는 로그 캣

06-30 18:00:36.950: E/AndroidRuntime(2226): FATAL EXCEPTION: main 
06-30 18:00:36.950: E/AndroidRuntime(2226): Process: com.example.smokecount, PID: 2226 
06-30 18:00:36.950: E/AndroidRuntime(2226): java.lang.RuntimeException: Unable to start receiver com.example.smokecount.Widget: java.lang.NullPointerException 
06-30 18:00:36.950: E/AndroidRuntime(2226):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2407) 
06-30 18:00:36.950: E/AndroidRuntime(2226):  at android.app.ActivityThread.access$1600(ActivityThread.java:135) 
06-30 18:00:36.950: E/AndroidRuntime(2226):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
06-30 18:00:36.950: E/AndroidRuntime(2226):  at android.os.Handler.dispatchMessage(Handler.java:102) 
06-30 18:00:36.950: E/AndroidRuntime(2226):  at android.os.Looper.loop(Looper.java:137) 
06-30 18:00:36.950: E/AndroidRuntime(2226):  at android.app.ActivityThread.main(ActivityThread.java:4998) 
06-30 18:00:36.950: E/AndroidRuntime(2226):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-30 18:00:36.950: E/AndroidRuntime(2226):  at java.lang.reflect.Method.invoke(Method.java:515) 
06-30 18:00:36.950: E/AndroidRuntime(2226):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 
06-30 18:00:36.950: E/AndroidRuntime(2226):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 
06-30 18:00:36.950: E/AndroidRuntime(2226):  at dalvik.system.NativeStart.main(Native Method) 
06-30 18:00:36.950: E/AndroidRuntime(2226): Caused by: java.lang.NullPointerException 
06-30 18:00:36.950: E/AndroidRuntime(2226):  at com.example.smokecount.Widget.onReceive(Widget.java:42) 
06-30 18:00:36.950: E/AndroidRuntime(2226):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2400) 
06-30 18:00:36.950: E/AndroidRuntime(2226):  ... 10 more 
+0

내게 대답하십시오. @pareshmayani –

답변

0

지능 transfering위한 부분 주석 해당 영역이다. 그래서 Activtty에 Extra ("key", intValue)를 넣고 getIntent(). getExtras(). getInt ("key")에 위젯을 받아야합니다.

그렇지 않으면 SharedPreferences를 사용하여 Int를 가져올 수 있습니다. 이전처럼.

그래서 활동에 말할 수

:

Bundle bundle = getIntent().getExtras(); 
int yourInt = bundle.getInt("count", 0); 

편집 이미 일한으로

, 오히려 대신 주어진 의도를 사용해야 위젯

Intent i=new Intent(Smoke_Count.this, Widget.class); 
i.putExtra("count", counter); 

과에 getIntent()의 경우 NullpointerException이 발생하므로 다음과 같이 처리하십시오.

Bundle bundle = intent.getExtras(); 
int yourInt = bundle.getInt("count", 0); 
+0

위젯 활동에서 onRecieve 메소드를 사용하는 중에 오류가 발생합니다. @mapo –

+0

LogCat을 게시 할 수 있습니까? – mapodev

+0

내 편집 된 게시물 @mapo –