我想在每5秒后重复调用一个方法,每当我想停止重复调用该方法时,我可能会停止或重新启动该方法的重复调用.
这是一些我想要实现的示例代码.请在这方面帮助我,我将非常感谢你.
private int m_interval = 5000; // 5 seconds by default, can be changed later
private Handler m_handler;
@Override
protected void onCreate(Bundle bundle)
{
...
m_handler = new Handler();
}
Runnable m_statusChecker = new Runnable()
{
@Override
public void run() {
updateStatus(); //this function can change value of m_interval.
m_handler.postDelayed(m_statusChecker, m_interval);
}
};
public void startRepeatingTask()
{
m_statusChecker.run();
}
public void stopRepeatingTask()
{
m_handler.removeCallbacks(m_statusChecker);
}
Run Code Online (Sandbox Code Playgroud) 我想在我的应用中显示Google广告,但它会unexpected namespace prefix xmlns在此代码中显示错误xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads".如果我删除此行,则显示Error parsing XML:unbound prefix.请帮我确定哪里出了问题.这是我的代码.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background_port"
tools:context=".MainActivity">
<LinearLayout xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="a151b801b7c1d6b"
ads:adSize="BANNER"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
ads:loadAdOnCreate="true"/>
</LinearLayout>
--------------------
---- More views ----
--------------------
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)