0
我想根據用戶選擇在特定時間間隔關閉我的應用程序。在特定時間間隔關閉android應用程序的有效方法
因此,我所做的是我顯示時間間隔的列表,並根據該列表項選擇我傳遞給處理程序和可運行和後延遲方法。 任何人都可以知道在特定時間從應用程序退出的不同且有效的方式。
這裏是我的代碼
setContentView(R.layout.audio_player);
mp = new MediaPlayer();
lv1=(ListView)findViewById(R.id.listView1);
Button b = (Button)findViewById(R.id.stop);
tv =(TextView)findViewById(R.id.tv);
lv1.setAdapter(new ArrayAdapter<String>(this,R.layout.testxml, lv_arr));
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// final TextView mTextView = (TextView)view;
switch (position) {
case 0:
j=2;
Toast t = Toast.makeText(Play.this, "2 min selected",Toast.LENGTH_SHORT);
t.show();
break;
case 1:
j=5;
Toast t1 = Toast.makeText(Play.this, "5 min selected ",Toast.LENGTH_SHORT);
t1.show(); break;
case 2:
j=10;
Toast t2 = Toast.makeText(Play.this, "10 min selected",Toast.LENGTH_SHORT);
t2.show(); break;
case 3:
j=15;
Toast t3 = Toast.makeText(Play.this, "15 min selected",Toast.LENGTH_SHORT);
t3.show();
break;
default:
// Nothing do!
}
}
});
try {
mp.setDataSource("http://94.55.45.123/bbc");
mp.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
handler=new Handler();
final Runnable r = new Runnable()
{
public void run()
{
myFinish();
}
};
handler.postDelayed(r, (60*1000)+j);
i++;
}
private void myFinish() {
// TODO Auto-generated method stub
Toast t1 = Toast.makeText(Play.this, "Music stop",Toast.LENGTH_SHORT);
t1.show();
mp.stop();
finish();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
});
}
你好朋友..只是告訴我是否有效的方法? –