0
這裏意圖不去登錄課時,否則條件爲真,和應用程序崩潰。我不知道爲什麼應用程序崩潰,如果我在其他屏幕上刪除了intent方法,那麼應用程序不會崩潰,但是當其他條件成立時我想要登錄屏幕,我在這裏錯過了什麼,請幫助我。意圖不會去我的登錄屏幕在其他塊
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());
}
});
}
}
在else塊控制進入其他條件塊時,然後應用程序崩潰。如果我刪除了這個「Intent intent2 = new Intent(GcmMessageHandler.this,Login.class); startActivity(intent2);」。那麼應用程序不會崩潰,但不去登錄屏幕,只是目的是,我想轉到登錄屏幕上別的博克。在這裏我正在實施通知的GCM,並且我只是在這裏寫下必要的代碼。
這裏是錯誤的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)
請將您的代碼格式化。使其更具可讀性。 – dit
在'startActivity()'之前添加'intent2 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);' – Sanjeet
嘗試使用getapplicationcontext();而不是GcmMessageHandler.this –