我无法从类中调用Main Activity的方法。 我从一个类调用Main Activity的方法然后它给出一个错误是无法在没有调用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();
}
}
答案 0 :(得分:0)
public void getActivityObjetM(){
mActivity=new MainActivity();
mActivity.notificationAdded(true);
}
以上代码是做错事的方法。
您可以使用本地广播管理员。
Intent intent = new Intent("notification_received");
intent.putExtra("status", value);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
在您的活动中
private BroadcastReceiver message = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//Do something with the intent
}
};
了解详情this