我想從另一個類中調用MainActivity中的notify()方法。這是我做的:來自非活動類別的Android通知
public class MyAlarmService extends Service{
@Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate()
{
// TODO Auto-generated method stub
super.onCreate();
}
@SuppressWarnings("static-access")
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
String title = "title";
String message = "message";
new MainActivity().Notify(title, message);
...
MainActivity:
public class MainActivity extends Activity {
NotificationManager manager;
Notification myNotication;
@Override
protected void onCreate(Bundle savedInstanceState) {
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
...
public void Notify(String notificationTitle, String notificationMessage){
Intent intent = new Intent("com.xxxx.app.MainActivity");
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, 0);
Notification.Builder builder = new Notification.Builder(MainActivity.this);
builder.setAutoCancel(false);
builder.setTicker("this is ticker text");
builder.setContentTitle("Notification");
builder.setContentText("Text");
builder.setSmallIcon(R.drawable.infoicon);
builder.setContentIntent(pendingIntent);
builder.setOngoing(true);
builder.setSubText("This is subtext...");
builder.setNumber(100);
builder.build();
myNotication = builder.getNotification();
manager.notify(11, myNotication);
}
...
但我得到一個錯誤信息:
致命異常:主要 過程:com.xxxx.app,PID: 10311 java.lang.RuntimeException:無法使用Intent {cmp = com.xxxx.app/.MyAlarmService}啓動服務[email protected]:java.lang.NullPointerException:嘗試在android.app的android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3027) 上的null對象引用 上調用虛擬方法'java.lang.String android.content.Context.getPackageName()'。在android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper上使用ActivityThread.-wrap17(ActivityThread.java) $ android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1442) .loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 引起:java.lang。 NullPointerException:嘗試在android上的android.content.ContextWrapper.getPackageName(ContextWrapper.java:133) 上的null對象引用 上調用虛擬方法'java.lang.String android.content.Context.getPackageName()'。 app.PendingIntent.getActivity(PendingIntent.java:305) at android.app.PendingIntent.getActivity(PendingIntent.java:272) at com.xxxx.app.MainActivity.Notify( MainActivity.java:304) at com.xxxx.app.MyAlarmService.onStart(MyAlarmService.java:41) at android.app.Service.onStartCommand(Service.java:459) at android.app.ActivityThread.handleServiceArgs( (Handler.Handler.dispatchMessage)(HandlerMessage(ActivityThread.java:1442) )在android.app.ActivityThread.-wrap17(ActivityThread.java) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1442) at android.os.Handler.dispatchMessage(Handler .java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main (ZygoteInit。Java的:616)
1)複製,在service.and通知方法調用它,無論你需要的。爲了觸發通知,通知服務需要Context或者Activity或者服務) – Chaitu
'Intent intent = new Intent(「com.xxxx.app.MainActivity」);'這是一種製作Intent的方法,但它很容易出錯。 –
除此之外,'new MainActivity()。Notify(title,message);'會導致錯誤,因爲您不應該執行'new MainActivity()',因爲它不受Android生命週期管理 –