我已经陷入这个问题3天了,我很沮丧..
我不知道为什么我的BroadCastReceiver无法正常工作......
我真诚地感谢解决我的问题...
这是我的代码..
MainActivity(我从我的整个代码中发布了一些代码......我认为这一点很重要) 这个MainActivity,我得到AlarmManager .. :
public class MainActivity extends FragmentActivity implements TabListener{
private GregorianCalendar mCalendar;
private NotificationManager mNotification;
private AlarmManager mManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNotification = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//Get AlarmManager here..
mManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
....
..
}
public NotificationManager getNotification(){
return mNotification;
}
public AlarmManager getAlarmManager(){
return mManager;
}
这是我的MsgBookingFragment(这个类扩展了Fragment ..):
//...somewhere else of my codes..
//Access to SQLDataBase and get Date, and set it in bookDate..
bookDate.set(Integer.parseInt(year.split("년")[0]),
Integer.parseInt(month.split("월")[0]),
Integer.parseInt(day.split("일")[0]),
Integer.parseInt(hourOfDay.split("시")[0]),
Integer.parseInt(minute.split("분")[0]));
//get mainActivity, in order to get AlarmManager..
final MainActivity mainActivity = (MainActivity)getActivity();
AlertDialog.Builder alert_confirm = new AlertDialog.Builder(getActivity());
alert_confirm.setMessage("예약 하시겠습니까?").setCancelable(false).setPositiveButton("예",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
doSomething();
msgViewService.write(vo);
Log.i("MsgBookingfragment", "msgViewInputVO write done = " + vo.toString());
FragmentManager FM = getFragmentManager();
FM.popBackStack();
FragmentTransaction ft = FM.beginTransaction();
mainActivity.setHashMapInsert(vo.getPersonID());
mainActivity.setBookedHashInsert(vo.getPersonID());
ft.replace(R.id.findperson, new FindPersonFragment()).commit();
//here, I get AlarmSet Instance and setAlarm..!!
AlarmSet alarm = new AlarmSet(bookDate,
getActivity().getApplicationContext(),
((MainActivity)getActivity()).getAlarmManager(),
((MainActivity)getActivity()).getNotification() );
alarm.setAlarm();
Toast.makeText(getActivity().getApplicationContext(), "예약되었습니다.", Toast.LENGTH_LONG).show();
}
这是我的AlarmSet类:
public class AlarmSet extends Activity implements OnDateChangedListener, OnTimeChangedListener{
private String tag = "AlarmSet";
private Calendar bookDate = Calendar.getInstance();
private Context context;
private AlarmManager mManager;
private NotificationManager mNotification;
public AlarmSet(Calendar cal, Context c, AlarmManager am, NotificationManager noti){
bookDate = cal;
context = c;
mNotification = noti;
mManager = am;
}
public void setAlarm() {
mManager.set(AlarmManager.RTC_WAKEUP, bookDate.getTimeInMillis(), pendingIntent());
Log.i("setAlarm : ", bookDate.getTime().toString());
//Log shows this message : Sat Oct 18 14:25:52 GMT+09:00 2014
}
//set free Alarm
public void resetAlarm() {
mManager.cancel(pendingIntent());
}
public PendingIntent pendingIntent() {
Intent i = new Intent(context, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getActivity(context, 3 , i, PendingIntent.FLAG_CANCEL_CURRENT);
Log.i(tag, "PendingIntent.......");
return pi;
}
这是我的AlarmReceiver:
public class AlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("TAg", "ggggggggggggggggggggg");
Toast.makeText(context, R.string.app_name, Toast.LENGTH_SHORT).show();
}
这是我的ManiFast:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.dduo.hrelation"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="org.dduo.hrelation.AlarmReceiver">
<intent-filter>
<action android:name="action"/>
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustResize"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这么长的代码......
顺便说一句,我认为它对pendingIntent方法如此怀疑......我想这个问题是因为那种方法......我从来没有见过&#34; ggggggggg&#34;在我的测试设备上记录msg ..和toast msg ..
请......帮帮我......
答案 0 :(得分:0)
in ManiFast :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.dduo.hrelation"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
//---> declare broadcast receiver like this
<receiver android:name="org.dduo.hrelation.AlarmReceiver">
</receiver>
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustResize"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
and pendingIntent():
public PendingIntent pendingIntent() {
Intent i = new Intent(context, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, i, 0);
Log.i(tag, "PendingIntent.......");
return pi;
}