发送int值从活动一至活动二
问题描述:
嗨,我想从一个活动发送int
价值活动二使用此代码发送int值从活动一至活动二
@Override
public void onClick(View v) {
new ChromaDialog.Builder()
.initialColor(getResources().getColor(R.color.colorAccent))
.colorMode(ColorMode.ARGB)
.indicatorMode(IndicatorMode.HEX)
.onColorSelected(new OnColorSelectedListener() {
@Override
public void onColorSelected(@ColorInt int color) {
Intent intent = new Intent(MainActivity.this, Hackpage.class);
intent.putExtra("intVariableName", color);
Toast.makeText(MainActivity.this,"color :"+Integer.toHexString(color),Toast.LENGTH_LONG).show();
}
})
.create()
.show(getSupportFragmentManager(), "ChromaDialog");
}
});
}
,但我在值i得到0
问题@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_hackpage);
Texthack = (TextView)findViewById(R.id.hacktext);
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
Texthack.setBackgroundColor(Color.parseColor("#"+Integer.toHexString(intValue)));
答
//global:
static final String SOME_ACTION = "com.example.test.myapplicationtest.SOME_ACTION";
IntentFilter intentFilter = new IntentFilter(SOME_ACTION); //youre project package name. example "com.example.yourprojectpackagename.SOME_ACTION".
@Override
public void onClick(View v) {
new ChromaDialog.Builder()
.initialColor(getResources().getColor(R.color.colorAccent))
.colorMode(ColorMode.ARGB)
.indicatorMode(IndicatorMode.HEX)
.onColorSelected(new OnColorSelectedListener() {
@Override
public void onColorSelected(@ColorInt int color) {
registerReceiver(mReceiver, intentFilter);
Intent intent = new Intent(SOME_ACTION);
intent.putExtra("intVariableName", color);
sendBroadcast(intent);
Toast.makeText(MainActivity.this,"color :"+Integer.toHexString(color),Toast.LENGTH_LONG).show();
}
})
.create()
.show(getSupportFragmentManager(), "ChromaDialog");
}
});
得到它}
///
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int value = intent.getIntExtra("intVariableName",0);
Toast.makeText(getApplicationContext(),""+value,Toast.LENGTH_LONG).show();
}
};
它的效果很好。 将价值发送到现有活动。 (例如MainActivity.java)。 所以你不需要使用Hackpage.class。
这是什么问题? –
你的实现似乎正确! – lukaspp
@VladMatvienko我得到0 intValue,而不是颜色值 – pic