Java 如何在android中从普通类调用主_活动的方法
嗨,我用这篇文章作为后台进程中的计时器 我的主要活动中的计时器代码是Java 如何在android中从普通类调用主_活动的方法,java,android,class,android-activity,Java,Android,Class,Android Activity,嗨,我用这篇文章作为后台进程中的计时器 我的主要活动中的计时器代码是 int repeatTime = 5; AlarmManager processTimer = (AlarmManager)getSystemService(ALARM_SERVICE); Intent intent = new Intent(this, processTimerReceiver.class); PendingIntent pendingIntent = PendingIntent.ge
int repeatTime = 5;
AlarmManager processTimer = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, processTimerReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
processTimer.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),repeatTime*1000, pendingIntent);
我在processTimerReceiver类中的代码是:
public class processTimerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
MainActivity m = new MainActivity();
m.get_start_info();
}
}
我想在主活动中调用方法get\u start\u info(),但应用程序崩溃
我的主要活动是:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
int repeatTime = 5;
AlarmManager processTimer = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, processTimerReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
processTimer.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),repeatTime*1000, pendingIntent);
get_start_info();
}
public void get_start_info() {
JsonArrayRequest movieReq = new JsonArrayRequest("www.example.com",
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
JSONObject obj = response.getJSONObject(0);
// any
sendNotification();
//any
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
AppController.getInstance().addToRequestQueue(movieReq);
}
public void sendNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(android.R.drawable.ic_dialog_email);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent intent = new Intent(MainActivity.this, azegar_mail_list.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
//mp.start();
builder.setContentIntent(pendingIntent);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
builder.setContentTitle("آزگار");
builder.setContentText("نامه جدید برای شما ارسال شده است");
builder.setAutoCancel(true);
builder.setSound(soundUri);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Will display the notification in the notification bar
notificationManager.notify(1, builder.build());
}
}
首先,如果在MainActivity之外调用
get\u start\u info()
,则将get\u start\u info()
和sendNotification
移出MainActivity
您将获得一个NullPointerException
,因为您试图使用MainActivity
的上下文在sendNotification
中构建通知,而该上下文为null,因为您正在使用new
关键字实例化MainActivity
(不应使用new关键字实例化新活动)。相反,将BroadcastReceiver
的上下文作为参数传递给sendNotification
,并使用它生成通知
使用上下文的代码部分
:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Intent intent = new Intent(MainActivity.this, azegar_mail_list.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
在这里,您不应该使用
this
,而应该使用作为参数传递的Context
。首先,如果您在MainActivity之外调用get\u start\u info()
,则将get\u start\u info()
和sendNotification
移出MainActivity
您将获得一个NullPointerException
,因为您试图使用MainActivity
的上下文在sendNotification
中构建通知,而该上下文为null,因为您正在使用new
关键字实例化MainActivity
(不应使用new关键字实例化新活动)。相反,将BroadcastReceiver
的上下文作为参数传递给sendNotification
,并使用它生成通知
使用上下文的代码部分
:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Intent intent = new Intent(MainActivity.this, azegar_mail_list.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
在这里,不要使用
这个
,而是应该使用作为参数传递的上下文
。嗨,merterpam,你的代码非常好。这段代码对我很有用,谢谢你,欢迎使用Stackoverflow=)。如果您对答案感到满意,并希望结束问题,请将答案标记为已接受。嗨,merterpam,您的代码非常好。此代码对我有效感谢Shi ebrehim,谢谢您,欢迎使用Stackoverflow=)。如果您对答案感到满意并想结束问题,请将答案标记为已接受。