ورود

View Full Version : اجراي يك كد در زمان مشخص از روز



reza_web
پنج شنبه 13 اسفند 1394, 06:18 صبح
سلام
من طبق آموزش اين لينك

http://barnamenevis.org/showthread.php?470763-%D9%86%D9%85%D8%A7%DB%8C%D8%B4-notification-%D8%B3%D8%B1-%D8%B3%D8%A7%D8%B9%D8%AA-%D8%AE%D8%A7%D8%B5&highlight=notification

خواستم آلارم جهت انجام كاري در يك ساعت مشخص انجام بدهم ولي اين پيغام را مي دهد

OnBootReceiver has no zero argument constructor

ميشه كسي كمك كنه؟

reza_web
شنبه 15 اسفند 1394, 03:44 صبح
من يك فايل جاوا بنام MyNotification ساختم و اين كدها را داخلش گذشتم


public class MyNotification extends BroadcastReceiver {
PendingIntent pi;
Intent intent;
NotificationManager notificationManager;
Notification notification;
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, MyService.class));
Log.d("AlarmController", "StartAlarm");
}
public class MyService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("AlarmController", "StartAlarm");

notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
intent = new Intent(this,MyNotification.class);
pi = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);


notification = new Notification.Builder(this)
.setContentTitle("عنوان")
.setContentText("متن اعلان")
.setSmallIcon(R.drawable.icon)
.setContentIntent(pi)
.setOngoing(true)
.build();
notificationManager.notify(0,notification);

return Service.START_FLAG_REDELIVERY;
//return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
public class AlarmController {
private Context m_Context;
private AlarmManager mgr;
private static final long PERIOD = 6000*60 ;
public AlarmController(Context context){
m_Context = context;
mgr = (AlarmManager)m_Context.getSystemService(Context.A LARM_SERVICE);
Log.d("AlarmController", "StartAlarm");
}


public void StartAlarm(){
StopAlarm();
Intent i = new Intent(m_Context, MyNotification.class);
PendingIntent pi=PendingIntent.getBroadcast(m_Context, 0,i, PendingIntent.FLAG_UPDATE_CURRENT);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAK EUP, SystemClock.elapsedRealtime(),PERIOD,pi);
Log.d("AlarmController", "StartAlarm");
}


public void StopAlarm(){
Intent i = new Intent(m_Context, MyNotification.class);
PendingIntent pi=PendingIntent.getBroadcast(m_Context, 0,i, PendingIntent.FLAG_UPDATE_CURRENT);
mgr.cancel(pi);
Log.d("AlarmController", "StopAlarm");
}
}
public class OnBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
AlarmController alarm = new AlarmController(context);
alarm.StartAlarm();
}
}
}


در مانيفست برنامه هم اينها را نوشتم


<receiver android:name=".MyNotification" ></receiver>
<receiver
android:name=".MyNotification$OnBootReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".MyNotification$MyService" >
</service>


اما وقتي موقع اجراي آلارم ميشه اين خطا را ميدهد:

MyNotification$MyService has no zero argument constructor

خيلي هم گشتم ولي چيزي پيدا نكردم

#root#
شنبه 15 اسفند 1394, 03:54 صبح
خيلي هم گشتم ولي چيزي پيدا نكردم
اگر میشه بگید چجوری گشتید.

ترجمش هم میگه ایراد چیه و چجوری باید رفع کرد.
کلاس MyService متد سازنده ای (constructor) که هیچ ورودی (argument) رو نپذیره نداره، پس یعنی باید یدونه ایجاد کنید.

reza_web
شنبه 15 اسفند 1394, 04:52 صبح
من هم در كلاس Myservice و هم OnBootReceiver اين دو متد را اضافه كردم





public MyService() {


}


و




public OnBootReceiver(){


}


نوع همه كلاسها را Static قرار دادم و مشكل حل شد
ممنون از شما