如何从静态类中调用非静态方法和非静态类

时间:2015-09-29 05:37:30

标签: java android service static broadcastreceiver

我知道这个问题曾多次提出过,而且有很多答案,但没有一个帖子解决了我的问题。 我已经实现了一个具有静态广播接收器作为内部类的服务。我从内部类调用非静态方法,但问题是非静态方法有一个数据库类的实例,它也不是静态的,我的日志显示给出了访问数据库的null异常。我试过传递上下文,但也没有解决问题。

public class AlarmServices extends Service {

    UserSQLiteDB dbHelper;
    Cursor cursor;
    String where = "1=1";


    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        scheduleAlarm();

    } 

    public void CallMethodAfter() {

        Log.v("call method after","yes");
        dbHelper = new UserSQLiteDB(AlarmServices.this);
        dbHelper.open();
        cursor = dbHelper.getInCompleteAttendance(where);

        // Daily Attendance
        int rows = cursor.getCount();
        if (rows > 0) {
            cursor.moveToFirst();

            for (int i = 0; i < rows; i++) {
                int Id;
                String recvdate, ffcode, terr_code, drcode, drname, addr, createddate, ffmgr, shift, lat, lon,droidrefno,locAddress, sync_status, curr_date, date_only;
                Id = cursor.getInt(0);
                recvdate = cursor.getString(1);
                ffcode = cursor.getString(2);
                terr_code = cursor.getString(3);
                drcode = cursor.getString(4);
                drname = cursor.getString(5);
                addr = cursor.getString(6);
                createddate = cursor.getString(7);
                ffmgr = cursor.getString(8);
                shift = cursor.getString(9);
                lat = cursor.getString(10);
                lon = cursor.getString(11);
                droidrefno = cursor.getString(12);
                locAddress = cursor.getString(13);
                sync_status = cursor.getString(14);
                curr_date = cursor.getString(15);
                date_only = cursor.getString(16);

                // Sync Server
                ChkInsertDailyAttendanceOperation insertAttd = new ChkInsertDailyAttendanceOperation(AlarmServices.this,
                        recvdate, ffcode, terr_code, drcode, drname, addr,
                        createddate, ffmgr,shift,Double.parseDouble(lat),Double.parseDouble(lon),locAddress,
                        droidrefno,curr_date,date_only);

                 insertAttd.execute(); 
                 cursor.moveToNext();

            }

        } else {

        }   
        dbHelper.close(); 
    }


    public void scheduleAlarm() {
        AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
        Intent alarmintent1 = new Intent(this, MyExtBroadcastReceiver.class);
        PendingIntent sender1=PendingIntent.getBroadcast(this, 100, alarmintent1, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);

        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.MINUTE,2);
        am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1000*2*60, sender1);
        System.out.println("alarm set...........");
    }

    public static class MyExtBroadcastReceiver extends BroadcastReceiver {

        public MyExtBroadcastReceiver() { 
             super();
        } 

        @Override
        public void onReceive(final Context context, final Intent intent) {         
            new AlarmServices().CallMethodAfter();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

1 个答案:

答案 0 :(得分:0)

我更愿意全面回答。

Android 3.0及更高版本中包含

LocalbroadcastManager,因此您必须使用支持库v4进行早期版本。请参阅此处的说明

制作广播接收器:

private BroadcastReceiver onNotice= new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        // intent can contain anydata
        Log.d("sohail","onReceive called");
        tv.setText("Broadcast received !");

    }
};

在onResume中注册您的接收器,例如:

protected void onResume() {
        super.onResume();

        IntentFilter iff= new IntentFilter(MyIntentService.ACTION);
        LocalBroadcastManager.getInstance(this).registerReceiver(onNotice, iff);
    }

//MyIntentService.ACTION is just a public static string defined in MyIntentService.

在onPause中取消注册接收器:

protected void onPause() {
  super.onPause();
  LocalBroadcastManager.getInstance(this).unregisterReceiver(onNotice);
}

每当从应用程序发送localbroadcast时都为No&#39;活动或服务,onReceive的{​​{1}}将被称为:)。

有关详细信息,请参阅http://www.example.com/shop