这是我的活动课:
public class activity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
Intent i = new Intent(activity.this, Service.class);
PendingIntent pIntent = PendingIntent.getService(getApplicationContext(), 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager aManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar c=Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY,mHour);
c.set(Calendar.MINUTE,mMinute);
c.set(year,month,date);
c.setTimeInMillis(System.currentTimeMillis());
aManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pIntent);
Toast.makeText(getApplicationContext(), "Service triggered", Toast.LENGTH_SHORT).show();
startService(i);
}
}
这是我的服务班级:
public class Smscreator extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent i, int flags, int startId) {
// TODO Auto-generated method stub
Context context=getApplicationContext();
Toast.makeText(context, "This is the service.",
Toast.LENGTH_LONG).show();
return START_STICKY;
}
}
这是我的清单类:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<application
<service
android:name=".Service"
android:enabled="true"
android:exported="true"></service>
<activity android:name=".activity"
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
变量mHour
,mMinute
,year
,month
和date
是取决于用户的日期和时间的某些值。
现在,当我执行此代码时,该服务立即被触发;它完全忽略了用户指定的日期和时间。有人可以帮我吗?
编辑:我目前正在使用Android M。