【问题标题】:How to resume a minimized android app from launch icon?如何从启动图标恢复最小化的 android 应用程序?
【发布时间】:2015-08-06 15:24:16
【问题描述】:

我是 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();
}

}  

应用程序运行良好,最小化后可以从最近的应用程序按钮访问应用程序,但无法从启动图标恢复。

请帮忙。

【问题讨论】:

    标签: android android-intent android-activity


    【解决方案1】:

    尝试检查 MainActivity isTaskRoot()。如果活动不是根活动 - 只需完成它。

    还可以尝试调试您的 MainActivity。好像您在第二次运行时发生了崩溃。尝试修复该问题或更改 MainActivity 的启动模式。

    【讨论】:

    • 刚刚尝试了 isTaskRoot() if (!isTaskRoot()) { final Intent intent = getIntent();最终字符串 intentAction = intent.getAction(); if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) { Log.w("TAG", "主活动不是根。完成主活动而不是启动。" );结束();返回; } } 但没有变化
    猜你喜欢
    • 1970-01-01
    • 2015-03-01
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    相关资源
    最近更新 更多