当前位置: 首页 > 知识库问答 >
问题:

当应用程序使用挂起的意图或其他解决方案在前台运行时,从主菜单打开特定选项卡

姜磊
2023-03-14

在我的应用程序中,首先有一个登录活动和一个家庭活动。在使用凌空截击登录并将参数传递给intent中的home activity之后,我能够启动一个前台服务,该服务通过通知在后台运行,并在pending intent的帮助下单击通知返回home activity。

现在,我正在搜索如何从主菜单打开应用程序,并直接访问带有前台服务待定意图的家庭活动。也许我应该将挂起意图的参数传递给登录活动,并检查它们以重定向到home活动,但我对此感到困惑,不明白。

以下是登录活动页面:

    EditText username, password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Button login = (Button) findViewById(R.id.signIn);
        // Login On Button Click
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            login();}
        });
// THIS IS THE SOLUTION THAT I THOUGHT ABOUT : if the intent is not null open HomeActivity
        Intent startinIntent = getIntent();
        if (startinIntent.getStringExtra("userLogged") != null && !startinIntent.getStringExtra("userLogged").isEmpty()) {
           String userLogged = startinIntent.getStringExtra("userLogged");
           Intent startAgain = new Intent(this, HomeActivity.class);
           tackBackWork.putExtra("userLogged", userLogged);
           startActivity(startAgain);
            Log.d("this is the ilue", userLogged);
        }
    }
    private void login(){
        username = (EditText)findViewById(R.id.username);
        password = (EditText)findViewById(R.id.password);
            RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
            JSONObject object = new JSONObject();
            try {
                //input your API parameters
                object.put("u",username.getText());
                object.put("p",password.getText());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            // Enter the correct url for your api service site
            String url = getResources().getString(R.string.loginUrl);
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, object,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            try{
                                String msg = response.getString("msg");
                                if(msg.contains("true")){
                                    Intent loggedIn = new Intent(LoginActivity.this, HomeActivity.class);
                                    loggedIn.putExtra("userLogged", response.toString());
                                    startActivity(loggedIn);
                                    finish();
                                }else{
                                    Toast.makeText(getApplicationContext(), "Identifiants Incorrectes", Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e){
                                Toast.makeText(getApplicationContext(), "erreur - 200 ", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getApplicationContext(), "Volley on error listener", Toast.LENGTH_SHORT).show();
                }
            });
            requestQueue.add(jsonObjectRequest);
        }
}

这里是家庭活动

public class HomeActivity extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        String userLogged = getIntent().getStringExtra("userLogged");
    }
    @Override
    protected void onStart() {
        super.onStart();
        Intent serviceIntent = new Intent(this, ForegroundServiceNoPopup.class);
        stopService(serviceIntent);
    }
    @Override
    public void onResume(){
        super.onResume();
        Intent serviceIntent = new Intent(this, ForegroundServiceNoPopup.class);
        stopService(serviceIntent);
    }
    @Override
    protected void onStop() {
        super.onStop();
        Intent serviceIntent = new Intent(this, ForegroundService.class);
        Intent intent = getIntent();
        String userLogged = intent.getStringExtra("userLogged");
        serviceIntent.putExtra("userLogged", userLogged);
        startService(serviceIntent);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Intent serviceIntent = new Intent(this, ForegroundService.class);
        Intent intent = getIntent();
        String userLogged = intent.getStringExtra("userLogged");
        serviceIntent.putExtra("userLogged", userLogged);
        startService(serviceIntent);
    }
    @Override
    protected void onPause() {
        super.onPause();
        Intent serviceIntent = new Intent(this, ForegroundService.class);
        Intent intent = getIntent();
        String userLogged = intent.getStringExtra("userLogged");
        serviceIntent.putExtra("userLogged", userLogged);
        startService(serviceIntent);
    }
}

这里是前台服务页面:

public class ForegroundService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String input = intent.getStringExtra("inputExtra");
        String userLogged = intent.getStringExtra("userLogged");
        Intent backToHomeActivity = new Intent(this, HomeActivity.class);
        backToHomeActivity.putExtra("userLogged", userLogged);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, backToHomeActivity, PendingIntent.FLAG_UPDATE_CURRENT);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Example Service")
               // .setLargeIcon()
               // .setColor()
                .setContentIntent(pendingIntent)
                .setContentText(input)
                .setSmallIcon(R.drawable.icon)
                .setContentIntent(pendingIntent)
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .build();

        startForeground(1, notification);
        return START_NOT_STICKY;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

我对这一切都不熟悉。请帮帮我。

共有1个答案

于飞飙
2023-03-14

解决方案是使用SharedReference。

public class LoginActivity extends AppCompatActivity {

    Button login;
    SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        login = (Button) findViewById(R.id.loginBtn);

        sp = getSharedPreferences("login",MODE_PRIVATE);

        if(sp.getBoolean("logged",false)){
            goToMainActivity();
        }

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToMainActivity();
                sp.edit().putBoolean("logged",true).apply();
            }
        });
    }

    public void goToMainActivity(){
        Intent i = new Intent(this,MainActivity.class);
        startActivity(i);
    }
}

请参阅下面的教程https://medium.com/@prakharsrivastava_219/keep-the-user-logged-in-android-app-5fb6ce29ed65

 类似资料:
  • 我有一个通知服务正在运行,它将为用户创建一个通知,如果用户单击该通知,它将打开一个活动以显示消息,我用于创建通知的方法是: MessageNotificationActivity正在使用“android:style/theme.dialog”使其看起来像一个对话框。现在的情况是,当我单击通知时,一切都很顺利,活动就像一个后台没有任何内容的对话框一样打开,但如果应用程序暂停并处于后台,当我单击通知时

  • 一切都很顺利,然后我运行我的代码,我得到了这个错误。 我已经尝试过以下解决方案(没有任何效果): http://stackoverflow.com/questions/22000423/javafx-and-maven-nullpointerexception-location-is-required 应用程序启动方法javafx gui中出现异常 应用程序启动方法中出现异常 JavaFX“loc

  • 仅当打开应用并执行通知单击时,通知单击才会启动指定的活动。如果应用程序处于后台/未运行,并且执行了通知单击,则会打开应用程序的 MainActivity。简而言之,这就像应用程序在活动堆栈之后正常打开,而不是在 PendingIntent 中打开指定的活动。 我想根据它们的类型将通知单击重定向到两个不同的活动(批准详细活动和对话详细活动)。 我使用FCM进行推送通知。我在这里粘贴我的Manifes

  • 问题内容: 我通过阅读一些博客和介绍材料开始使用docker。 我的理解是docker可以将单个应用程序包装到标准化容器中。容器提供了一个沙箱,应用程序需要运行的所有必需资源,并且内部的应用程序始终位于该容器中。这意味着我可以将容器运送到任何地方(不同类型的OS甚至是云平台),并且仍然可以正确运行。 如果我的理解是正确的,那是否意味着微软可以将其办公服包装到一个容器中,并且可以在mac os或li

  • 我知道这种类型的问题已经被问过多次在这个论坛上但我已经看了无数的变化的问题并尝试了许多不同的解决方案没有一个是工作的所以我想我会粘贴我的实际代码的情况下我是完全错过了什么。所以我正在访问CRM的API并接收json输出。产出如下: 这是我在使用cURL发布一个URL后得到的输出,该URL允许生成一个refresh_令牌,这可以在json输出中看到。我必须解析json以获取新的刷新令牌,以便在令牌过