내 앱에 삽입 광고를 구현하려고합니다. 임자는 postDelayed와 함께 처리기를 사용하여 삽입 광고의 게재를 10 초 지연합니다.Runnable에 하나의 처리기를 허용 하시겠습니까?
내 작은 응용 프로그램 조각을 사용합니다. 조각 교체 트랜잭션을 통해 조각 2로 이동하는 단추가있는 조각 1이 있습니다.
동일한 과제의 인스턴스가 여러 개 만들어져 두 개 이상의 삽입 광고가 게재되는 경우가 있습니다 (불만). 이것은 조각 2에 들어가서 뒤로 누르고 조각 2로 다시 들어가면 발생합니다.
내가 원하는 것은이 실행 파일에 대해 단일 처리기 (1 개의 메시지) 만 허용하는 것입니다.
public class MainActivity extends ActionBarActivity {
private ViewGroup container;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState == null)
{
container = (ViewGroup)findViewById(R.id.container);
if(container != null)
{
Fragment1 fragment1 = new Fragment1();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(container.getId(), fragment1, Fragment1.class.getName());
fragmentTransaction.commit();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onResume()
{
// TODO Auto-generated method stub
super.onResume();
int isAvaiable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if(isAvaiable == ConnectionResult.SUCCESS)
{
Log.d("TEST", "GPS IS OK");
}
else if(isAvaiable == ConnectionResult.SERVICE_MISSING ||
isAvaiable == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED ||
isAvaiable == ConnectionResult.SERVICE_DISABLED)
{
GooglePlayServicesUtil.getErrorDialog(isAvaiable, this, 10).show();
}
}
}
조각 (삽입 광고를 포함) 1
public class Fragment1 extends Fragment implements OnClickListener {
private Button bFragmentB;
private ViewGroup container;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_1, container, false);
this.container = container;
bFragmentB = (Button)view.findViewById(R.id.bFragmentB);
bFragmentB.setOnClickListener(this);
return view;
}
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
switch(arg0.getId())
{
case R.id.bFragmentB:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(container.getId(),new Fragment2(),Fragment2.class.getName());
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
break;
}
}
}
조각이에게 내 주요 활동
나타나는에서 여러 개의 광고를 방지하기 위해 여기에public class Fragment2 extends Fragment
{
private Handler handler = new Handler();
private InterstitialAd interstitialAd;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_2, container, false);
return view;
}
@Override
public void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
interstitialAd = new InterstitialAd(getActivity());
interstitialAd.setAdUnitId(getResources().getString(R.string.banner_ad_unit_id));
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
interstitialAd.loadAd(adRequest);
interstitialAd.setAdListener(new AdListener()
{
@Override
public void onAdLoaded()
{
handler.postDelayed(showInterstitialAd, 10000);
}
});
}
private Runnable showInterstitialAd = new Runnable()
{
@Override
public void run()
{
interstitialAd.show();
}
};
}
는 요약입니다 내가 시도했지만 일하지 않은 것들 중 단편 B 형들의 OnDestroy에
나는
@Override
public void onDestroy()
{
super.onDestroy();
handler.removeCallbacks(showInterstitialAd);
}
아직도 작업의 1 개 이상의 인스턴스에서 결과이 코드를 (이 개 광고에 나오는)이있다. 내 이해에서 작업이 파괴 될 것이기 때문에 화면이 어쨌든 회전하면 좋지 않습니다.
이로 시작하는 메시지가없는 것 때문에 시작되어 새로운 투수 객체를 고려 이것은 꽤 바보 일
interstitialAd.setAdListener(new AdListener()
{
@Override
public void onAdLoaded()
{
if(!handler.hasMessages(1))
{
handler.sendEmptyMessage(1);
handler.postDelayed(showInterstitialAd, 10000);
}
//else do nothing
}
});
에서 또 다른 시도 : D를. 아이디어는 1의 대기열에 메시지를 가지고 있었고 1이 이미 존재하면 다른 처리기를 시작하지 않는 것이 었습니다.
답장을 보내 주셔서 감사합니다. 문제가 계속되는 경우 계속 추가하십시오. – user3364963
내 업데이트 – ashoke
이 (가) 글로벌 변수 – ashoke