我是Android编程的初学者,我正在创建一个音乐应用程序,我的应用程序面临的问题是当我的应用程序最小化并且我尝试从启动图标恢复应用程序时,启动画面会出现指定的时间但是之后,android主屏幕出现,当我尝试再次打开它同样的事情发生,再次启动应用程序我必须强制停止应用程序。
我在主要活动中所做的是找到设备是否具有互联网连接,该应用程序将流式播放在线音乐,但如果没有互联网连接,该应用将播放下载的音乐。
主要活动代码如下:
public class MainActivity extends Activity {
// flag for Internet connection status
Boolean isInternetPresent = false;
Boolean responseCode = false;
// Connection detector class
ConnectionDetector cd;
int code;
int splashtime=6000;
Thread splash,t;
ProgressBar pb1;
Handler hd;
SharedPreferences appPreferences;
boolean isAppInstalled = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
appPreferences = PreferenceManager.getDefaultSharedPreferences(this);
isAppInstalled = appPreferences.getBoolean("isAppInstalled",false);
if(isAppInstalled==false){
/**
create short code
*/
Intent shortcutIntent = new Intent(getApplicationContext(),MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getResources().getString(R.string.app_name));
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon));
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(intent);
/**
Make preference true
*/
SharedPreferences.Editor editor = appPreferences.edit();
editor.putBoolean("isAppInstalled", true);
editor.commit();
}
setContentView(R.layout.activity_main);
// creating connection detector class instance
cd = new ConnectionDetector(getApplicationContext());
/**
Check Internet status button click event
*/
// get Internet status
isInternetPresent = cd.isConnectingToInternet();
if (isInternetPresent) {
// Internet Connection is Present
// make HTTP requests
}
else {
// Internet connection is not present
// Ask user to connect to Internet
Toast.makeText(getApplicationContext(), "You don't have internet connection.\n Opening Offline Player...", Toast.LENGTH_LONG).show();
}
pb1=(ProgressBar)findViewById(R.id.progressBar1);
hd=new Handler();
splash=new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
//pb1.setVisibility(View.VISIBLE);
synchronized(this)
{
try {
wait(splashtime);
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
// check for Internet status
if (isInternetPresent) {
// Internet Connection is Present
// make HTTP requests
Intent i=new Intent();
i.setClass(MainActivity.this, MusicPlayerActivity.class);
startActivity(i);
}
else {
// Internet connection is not present
// Ask user to connect to Internet
// Offline Palyer
Intent i = new Intent(getApplicationContext(), OfflineMusicPlayer.class);
startActivityForResult(i, 100);
}
}
}
}});
splash.start();
}
}
应用程序正常工作,当最小化时,可以从最近的应用程序按钮访问该应用程序,但无法从启动图标恢复该应用程序。
请帮忙。
答案 0 :(得分:0)
尝试签入MainActivity isTaskRoot()。如果活动不是root活动 - 只需完成它。
还尝试调试MainActivity。好像你在第二次跑步时发生了撞车事故。尝试修复它或更改MainMctivity的launchMode。