我想知道我handler.post(runnable);应该何时使用,何时使用
new Thread(runnable).start();
在Handler的开发人员文档中提到:
导致Runnable r添加到消息队列中.runnable将在连接此处理程序的线程上运行.
这是否意味着,如果我写onCreate()的Activity类:
Handler handler = new Handler();
handler.post(runnable);
Run Code Online (Sandbox Code Playgroud)
然后runnable将在一个单独的线程或Activity的线程中调用?
在我正在使用的一项活动中AsyncTask.在doInBackground()我做各种方法的调用.在其中一个方法中我得到一个异常,所以在catch块中我想在Toast中显示错误.我知道我可以使用Log,但我仍然喜欢Toast.那么,如何在doInBackground()中的AsyncTask中使用Toast?
我知道这个问题之前已被多次询问,但任何解决方案都不起作用,我的情况有点不同.
我Activity可以从许多不同的活动中调用它.但是我希望当用户按下后退按钮而不是之前的活动时,app应该转到主屏幕.
一种方法,StartActivityFromResult()但我必须在每个调用Activity中使用它.
我正在尝试获取手机中的所有音乐文件:
为此,我正在使用:
String[] STAR = {"*"};
Uri allExternalSongUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
Cursor cursor = getContentResolver().query(allExternalSongUri, STAR, selection, null, null);
if(cursor != null){
if(cursor.moveToFirst()){
do {
String songName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
Log.i("name", songName);
} while (cursor.moveToNext());
}
cursor.close();
}
Run Code Online (Sandbox Code Playgroud)
但是上面的代码,除了获取音乐文件之外,还提取了一些额外的不必要的文件,如*sound_screen_on.mp3*(由其他应用程序安装和使用).
问题是我的原生Android音乐播放器没有列出和播放这些不必要的文件.
如何过滤这些文件.
我正在尝试检查java中wait/notify的工作原理.
码:
public class Tester {
public static void main(String[] args) {
MyRunnable r = new MyRunnable();
Thread t = new Thread(r);
t.start();
synchronized (t) {
try {
System.out.println("wating for t to complete");
t.wait();
System.out.println("wait over");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class MyRunnable implements Runnable {
public void run() {
System.out.println("entering run method");
synchronized (this) {
System.out.println("entering syncronised block");
notify();
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("leaving syncronized block");
}
System.out.println("leaving …Run Code Online (Sandbox Code Playgroud) 在应用程序中,只要片段失去焦点(即在其上打开另一个活动/片段),就会onSaveInstanceState()调用它.
同样是在提到开发者指南.
我正在尝试使用这个approch来恢复我的片段的状态.我的目的是把这种bundle在onActivityCreated(),当恢复片段.
虽然onSaveInstanceState在片段失去焦点之前被调用.但是,当onActivityCreated()它被调用时,它会将Bundle savedInstanceStateas 还原为null.
如何从捆绑包中获取数据.
码:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
MyListAdapter adapter = new MyListAdapter(getActivity(),
R.layout.my_row, titles, icons, this);
setListAdapter(adapter);
if (savedInstanceState != null) {
// Never goes inside this condiiton.
// Restore last state for checked position.
mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("curChoice", mCurCheckPosition);
}
Run Code Online (Sandbox Code Playgroud) 我使用的是JSON与RESTAPI使用Web服务。
现在我还需要在请求中发送图像。是否可以?
如果是,我需要在客户端/服务器端做哪些更改。
在我的 Java 代码中,我应该如何发送图像内容(是否需要单独设置内容类型)?
我创建了一个ListView正在使用的FastScroll.(参见图片)当用户点击以下任何按钮(即所有曲目,艺术家,专辑)时,每次调用以下自定义ArrayAdater时
ArrayAdapter<String> adapter = new ScrollIndexListAdapter(Listing.this, elements);
//Code for ScrollIndexListAdapter is below
Run Code Online (Sandbox Code Playgroud)
并更新相同的ListView.

问题:根据我在Android中的调查,该getSections()方法只被调用一次(即仅在第一次调用ScrollIndexListAdapter时).
这次填充部分并且fastScrolling完美地工作.
但是当我通过单击Artists/Album更新ListView时,getSections()不会调用该方法.因此使用较旧的部分,FastScrolling仍然显示旧字母的预览.
那么,每当ListView更新时,我怎样才能使部分更新?
有一种setSections()方法,但我无法找到如何使用它.
ScrollIndexListAdapter类的代码:
public class ScrollIndexListAdapter extends ArrayAdapter implements
SectionIndexer {
// Variables for SectionIndexer List Fast Scrolling
HashMap<String, Integer> alphaIndexer;
String[] sections;
private static ArrayList<String> list = new ArrayList<String>();
public ScrollIndexListAdapter(Context context, ArrayList<String> list) {
super(context, android.R.layout.simple_list_item_1, android.R.id.text1,
list);
this.list.clear();
this.list.addAll(list);
/*
* Setting SectionIndexer
*/
alphaIndexer = new HashMap<String, …Run Code Online (Sandbox Code Playgroud) 如何在FragmentTabHost中显示片段的状态?
感谢本教程,我能够FragmentTabHost在我的应用程序中实现.
我的目的是创建一个应用程序,其主要活动包含一些选项卡(在整个应用程序的顶部).单击每个选项卡将在选项卡下方打开一个新片段.
问题是当我单击选项卡时执行某些操作,然后转到另一个选项卡,该选项卡将打开一个新的片段,然后返回到第一个选项卡 - 此处的更改不会保留.
流:

我真的需要实现这个逻辑.如果我的方法有误,请建议替代方案.
谢谢
码:
主要活动
public class FagTabHostMain extends FragmentActivity {
FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fag_tab_host_main);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("audio").setIndicator("Audio"),
AudioContainerFragmentClass.class, null);
mTabHost.addTab(mTabHost.newTabSpec("video").setIndicator("Video"),
VideoContainerFragmentClass.class, null);
}
@Override
public void onBackPressed() {
boolean isPopFragment = false;
String currentTabTag = mTabHost.getCurrentTabTag();
if (currentTabTag.equals("audio")) {
isPopFragment = ((AudioContainerFragmentClass) getSupportFragmentManager()
.findFragmentByTag("audio")).popFragment();
} else if (currentTabTag.equals("video")) {
isPopFragment = ((VideoContainerFragmentClass) getSupportFragmentManager()
.findFragmentByTag("video")).popFragment();
}
// Finish when …Run Code Online (Sandbox Code Playgroud) 我的要求是创建一个服务,该服务在特定间隔后继续检查队列并处理队列中的元素.
对于10秒后的调度任务,我正在使用:
ScheduledExecutorService schd = Executors.newSingleThreadScheduledExecutor();
schd.scheduleAtFixedRate(readQueueRunnable, 10, 10, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
我的问题是假设第一次,在队列中有许多元素,我的单线程开始处理队列.
即使10秒后我的第一个线程仍在执行它.
所以当10秒后再次调用runnable时,它会停止前一个执行线程并启动新线程.或者,它将检查线程是否已经在运行,如果它正在运行,那么在这种情况下它将跳过Runnable内部.