我無法從類中調用主要活動的方法。 我從一個類調用主要活動的方法,那麼它提供了一個錯誤是無法創建內螺紋處理程序尚未調用Looper.prepare()我無法從類中調用主要活動的方法
這是我的代碼
package com.example.romil.mypushexample;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
* Created by romil on 22/4/17.
*/
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
String message;
String imageUri;
Bitmap bitmap;
String TrueOrFlase;
API realtrackerApi;
UserSessionManager sessionManager;
MainActivity mActivity;
public void getActivityObjetM(){
mActivity=new MainActivity();
mActivity.notificationAdded(true);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO: Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated.
Log.d(TAG, "From: " + remoteMessage.getFrom());
//Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
// Log.i(TAG,remoteMessage.getNotification().getBody());
realtrackerApi=GlobalMethods.getRealtrackerAPI(this);
sessionManager=new UserSessionManager(this);
remoteMessage.getData();
Log.i(TAG, "Message data payload: " + remoteMessage.getData());
if (remoteMessage.getData().size() > 0) {
Log.i(TAG, "Message data payload: " + remoteMessage.getData());
message = remoteMessage.getData().get("message");
imageUri = remoteMessage.getData().get("image");
bitmap = getBitmapfromUrl(imageUri);
TrueOrFlase = remoteMessage.getData().get("AnotherActivity");
sendNotification(message, bitmap, TrueOrFlase);
getActivityObjetM();
}
if (remoteMessage.getNotification() != null) {
Log.i(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
}
private void sendNotification(String messageBody, Bitmap image, String TrueOrFalse) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("AnotherActivity", TrueOrFalse);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setLargeIcon(image)/*Notification icon image*/
.setSmallIcon(R.drawable.testimage1)
.setContentTitle(messageBody)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(image))/*Notification with Image*/
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
public Bitmap getBitmapfromUrl(String imageUrl) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
return bitmap;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
這是我的主要活動
package com.example.romil.mypushexample;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.messaging.FirebaseMessaging;
import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import static java.security.AccessController.getContext;
public class MainActivity extends AppCompatActivity {
ListView listView;
UserSessionManager sessionManager;
API realtrackerApi;
ArrayList<GetNotificationListOutput> data;
MyFirebaseMessagingService myFirebaseMessagingService;
int listItemCount;
Notification_Adapter adapter;
boolean notificationStatus=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.notification_listView);
sessionManager=new UserSessionManager(getApplicationContext());
realtrackerApi=GlobalMethods.getRealtrackerAPI(this);
// message=myFirebaseMessagingService.getMessage();
// image=myFirebaseMessagingService.getImage();
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(getApplicationContext(),Push_Notification.class);
startActivity(i);
}
});
findViewById(R.id.showNotification).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RetroCallForGettingNotificationList();
listView.setVisibility(View.VISIBLE);
}
});
findViewById(R.id.refreshNotification).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RetroCallForGettingNotificationList2();
}
});
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
String value = getIntent().getExtras().getString(key);
if (key.equals("AnotherActivity") && value.equals("True")) {
Intent intent = new Intent(this, AnotherActivity.class);
intent.putExtra("value", value);
startActivity(intent);
finish();
}
}
}
subscribeToPushService();
}
private void subscribeToPushService() {
FirebaseMessaging.getInstance().subscribeToTopic("news");
Log.d("AndroidBash", "Subscribed");
Toast.makeText(MainActivity.this, "Subscribed", Toast.LENGTH_SHORT).show();
String token = FirebaseInstanceId.getInstance().getToken();
// Log and toast
Log.d("AndroidBash", token);
Toast.makeText(MainActivity.this, token, Toast.LENGTH_SHORT).show();
}
public void notificationAdded(boolean status){
RetroCallForGettingNotificationList2();
}
}
'mActivity =新MainActivity();'這是不可能的/不允許以這種方式創建活動的一個實例。不可能用'new'。 'mActivity.notificationAdded(true);'如果你讓這個方法是靜態的,你可以用'MainActivity.notificationAdded(true);'調用它。但如果這個我不知道的任何用途。我認爲它不會。 – greenapps
請[編輯]您的代碼,以減少它到你的問題[mcve]。您當前的代碼包含很多與您的問題相關的代碼 - 通常,最小樣本看起來與單元測試相似:只執行一項任務,輸入值指定爲可重現性。 –