Android 如何从活动到小部件显示一个整数?
我必须在我的应用程序中添加活动。一个活动用于增加计数值,另一个活动扩展AppWidget。因为我需要定期在应用程序小部件中显示计数值(整数) 这是我的活动代码 Smoke_Count.javaAndroid 如何从活动到小部件显示一个整数?,android,android-widget,Android,Android Widget,我必须在我的应用程序中添加活动。一个活动用于增加计数值,另一个活动扩展AppWidget。因为我需要定期在应用程序小部件中显示计数值(整数) 这是我的活动代码 Smoke_Count.java package com.example.smokecount; import android.os.Bundle; import android.preference.PreferenceManager; import android.app.Activity; import android.conte
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();
}*/
}
注释的区域是传递int的部分。因此您需要在Activty中输入extra(“key”,intValue),并在getIntent().getExtras().getInt(“key”)上的小部件中接收int 否则,您可以像以前一样使用SharedReferences获取Int 因此,让我们在活动中说:
Intent i=new Intent(Smoke_Count.this, Widget.class);
i.putExtra("count", counter);
在小部件中
Bundle bundle = getIntent().getExtras();
int yourInt = bundle.getInt("count", 0);
编辑
正如您已经使用过的,您应该使用给定的intent而不是getIntent(),这会导致NullpointerException,请执行以下操作:
Bundle bundle = intent.getExtras();
int yourInt = bundle.getInt("count", 0);
回答我先生@Pareshmayan在widget activity@mapocan中使用OnReceive方法时出错。你可以发布日志吗?你能告诉我widget.java中第42行有什么吗?这是第42行的代码int yourInt=bundle.getInt(“count”,0)@马波
Bundle bundle = getIntent().getExtras();
int yourInt = bundle.getInt("count", 0);
Bundle bundle = intent.getExtras();
int yourInt = bundle.getInt("count", 0);