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 go go login screen when else condition true, what i am missing here pls help me.
package notification;
//import goes here
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() {
// TODO Auto-generated method stub
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)
//**********************for custom
.setVibrate(vibrate)
//**********************for custom
.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,. 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. here i am implementing GCM for Notification, and i am just write here necessary code in question.
here is logcat for errors:
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)