简体   繁体   English

Intent不会在else块中进入我的登录屏幕

[英]Intent doesn't go my login screen in else block

Here intent no go on login class when else condition true, and app get crashed. 如果其他条件为真,并且应用程序崩溃,则此处意图不进行登录。 I don't know why app crashed and if I removed the intent method in else screen then app not crashed, but I want to go to login screen when else condition true, What am I missing here please help me. 我不知道为什么应用程序崩溃了,如果我在其他屏幕上删除了intent方法,然后应用程序没有崩溃,但是当其他条件为true时,我想进入登录屏幕,请问我在这里缺少什么。

public class GcmMessageHandler extends IntentService {
    PendingIntent alarmIntent;
    GoogleCloudMessaging gcm;
    String regid;
    SessionManager session;

    NotificationManager nm;
    static int UniqueID=2154;

    String mes,message,call,detailid,platform,service,alert,userid;
    int  U_ID=0;

    private Handler handler;
    public GcmMessageHandler() {
        super("GcmMessageHandler");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        handler = new Handler();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();

        /**
        * Get stored session data userid from other class
        * */
        userid = "";
        session = new SessionManager(getApplicationContext())
        HashMap<String, String> user = session.getUserDetails();
        userid = user.get(SessionManager.KEY_userid);

        if(userid != null && ! userid.trim().isEmpty())
        {
            U_ID = Integer.parseInt(userid);
            Toast.makeText(GcmMessageHandler.this, "converted="+U_ID, Toast.LENGTH_LONG).show();
        }

        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

        String messageType = gcm.getMessageType(intent);


        alert = extras.getString("alert");
        message = extras.getString("message");
        call = extras.getString("call");
        detailid = extras.getString("id");
        platform = extras.getString("platform");
        service = extras.getString("service");

        showToast();
        //Log.i("GCM", "Received : (" +messageType+")  "+extras.getString("title"));

        GcmBroadcastReceiver.completeWakefulIntent(intent);
        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    }

    public void showToast(){        
        handler.post(new Runnable() {

            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @SuppressLint("NewApi")
            public void run() {
                Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                //builder.setSound(alarmSound);

                long[] vibrate = { 0, 100, 200, 300 };

                NotificationManager mNotificationManager;
                NotificationCompat.Builder builder;
                mNotificationManager = (NotificationManager) GcmMessageHandler.this.getSystemService(Context.NOTIFICATION_SERVICE);

                if(call.equalsIgnoreCase("newsletter")){
                    Intent intent = new Intent(GcmMessageHandler.this, Webview_news.class);         
                    intent.putExtra("id",detailid);
                    intent.putExtra("platform",platform);
                    intent.putExtra("service",service);
                    alarmIntent = PendingIntent.getActivity(GcmMessageHandler.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);     
                }

                if(U_ID>0){
                    if(call.equalsIgnoreCase("tipdetail")) {
                        Intent intent = new Intent(GcmMessageHandler.this, Selection.class);         
                        intent.putExtra("id",detailid);
                        intent.putExtra("platform",platform);
                        intent.putExtra("service",service);
                        alarmIntent = PendingIntent.getActivity(GcmMessageHandler.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);     
                    }
                    else if(call.equalsIgnoreCase("offers")) {
                        Intent intent = new Intent(GcmMessageHandler.this, Selection.class);         
                        //intent.putExtra("sms",message);
                        intent.putExtra("id",detailid);
                        intent.putExtra("platform",platform);
                        intent.putExtra("service",service);
                        alarmIntent = PendingIntent.getActivity(GcmMessageHandler.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);     
                    }
                }
                else if(userid==null || userid.equals("0")) {
                    Toast.makeText(getApplicationContext(),"welcome to login class", Toast.LENGTH_LONG).show();

                    //Here in intent app goes crashed and not going to login screen, this is my problem 
                    Intent intent2 = new Intent(GcmMessageHandler.this, Login.class);
                    startActivity(intent2);
                }


                NotificationCompat.Builder mBuilder = 
                    new NotificationCompat.Builder(GcmMessageHandler.this)
                            .setSmallIcon(R.drawable.notify)
                            .setContentTitle(alert)
                            .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(message))
                            .setSound(alarmSound)
                            .setVibrate(vibrate)
                            .setAutoCancel(true)
                            .setContentText(call);

                mBuilder.setContentIntent(alarmIntent);
                // Notification note = mBuilder.build();
                // note.defaults |= Notification.DEFAULT_VIBRATE;

                Notification notification = new Notification();
                notification. defaults |= Notification.DEFAULT_VIBRATE;

                UniqueID++;
                mNotificationManager.notify(UniqueID, mBuilder.build());
            }
        });
    }
}

In else block when control goes in else condition block then app get crashed,. 在else块中,当控件进入else条件块时,则应用崩溃。 If i Remove this " Intent intent2 = new Intent(GcmMessageHandler.this, Login.class); startActivity(intent2);".then app get not crashed but not go login screen, just simply the purpose is that i want goto login screen on else bock. 如果我删除此“ Intent intent2 = new Intent(GcmMessageHandler.this,Login.class); startActivity(intent2);”。那么应用程序不会崩溃,但不会进入登录屏幕,只是目的是我要转到登录屏幕否则博克。 here i am implementing GCM for Notification, and i am just write here necessary code in question. 在这里,我正在实施GCM for Notification,而我只是在这里编写有问题的必要代码。

here is logcat for errors: 这是错误的logcat:

11-02 11:16:46.690: D/AndroidRuntime(30389): Shutting down VM
   11-02 11:16:46.690: W/dalvikvm(30389): threadid=1: thread exiting with  uncaught exception (group=0x411ae2e8)
   11-02 11:16:46.760: E/AndroidRuntime(30389): FATAL EXCEPTION: main
   11-02 11:16:46.760: E/AndroidRuntime(30389): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
   11-02 11:16:46.760: E/AndroidRuntime(30389):     at android.app.ContextImpl.startActivity(ContextImpl.java:1106)
   11-02 11:16:46.760: E/AndroidRuntime(30389):     at android.app.ContextImpl.startActivity(ContextImpl.java:1100)
   11-02 11:16:46.760: E/AndroidRuntime(30389):     at android.content.ContextWrapper.startActivity(ContextWrapper.java:305)
   11-02 11:16:46.760: E/AndroidRuntime(30389):     at notification.GcmMessageHandler$1.run(GcmMessageHandler.java:173)
   11-02 11:16:46.760: E/AndroidRuntime(30389):     at android.os.Handler.handleCallback(Handler.java:615)
   11-02 11:16:46.760: E/AndroidRuntime(30389):     at android.os.Handler.dispatchMessage(Handler.java:92)
   11-02 11:16:46.760: E/AndroidRuntime(30389):     at android.os.Looper.loop(Looper.java:174)
   11-02 11:16:46.760: E/AndroidRuntime(30389):     at android.app.ActivityThread.main(ActivityThread.java:4952)
   11-02 11:16:46.760: E/AndroidRuntime(30389):     at java.lang.reflect.Method.invokeNative(Native Method)
   11-02 11:16:46.760: E/AndroidRuntime(30389):     at java.lang.reflect.Method.invoke(Method.java:511)
   11-02 11:16:46.760: E/AndroidRuntime(30389):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
   11-02 11:16:46.760: E/AndroidRuntime(30389):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
   11-02 11:16:46.760: E/AndroidRuntime(30389):     at    dalvik.system.NativeStart.main(Native Method)

Add new task launch mode. 添加新的任务启动模式。

Intent intent2 = new Intent(GcmMessageHandler.this, Login.class);
intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // add this flag
startActivity(intent2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM