为什么我的闹钟应用无法正常工作?

时间:2015-06-10 07:32:21

标签: java android clock alarm

我在LogCat中没有看到任何错误。不幸的是警报不起作用。编译工作正常。请帮帮我!

MainActivity.java

的onCreate

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.note_layout);
    find_View();

    helper=new SQLite_set(Create_activity.this,"table_name", null, 0);
    db=helper.getReadableDatabase();
    myCursor=db.rawQuery("select note_id from table_name ;", null);


    rd_btn1.setChecked(true);
    select_time.setOnCheckedChangeListener(select_timeOnCheckedChange);
    select_button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub




            initEndDateTime = date.format(new java.util.Date());
            System.out.println("@@@@@@"+initEndDateTime);
            DateTimePick dateTimePicKDialog = new DateTimePick(  
            Create_activity.this, initEndDateTime);  
            dateTimePicKDialog.dateTimePicKDialog(timeShow);
            timeShow.setTextColor(0xffaa0000);



        }
    });

按钮

checked.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(title.getText().toString()!=null){
                if(content.getText().toString()!=null){
                    if(select_witch==1){
                        if(reciprocal.getText().toString()!=null){

                            AddJob();


                        }else
                            show_info("時間未填寫");

                    }else 
                        if(timeShow.getText().toString()!=null){

                            AddJob();
                        }else
                            show_info("時間未選擇");
                }else
                    show_info("內容未填寫");
            }else
                show_info("標題未填寫");




        }
    });

添加数据

private void AddJob(){
    String now_date;
     String now_time;
    if(select_witch!=1){
     now_date = spliteString(timeShow.getText().toString(), "日", "index", "front"); // 日期  

     now_time = spliteString(timeShow.getText().toString(), "日", "index", "back"); // 时间  
    }else{
        now_date=today.format(new java.util.Date());

        now_time=reciprocal.getText().toString();
        System.out.println("@22222@"+now_time);
    }

        long check=date_db.insert( title.getText().toString(), content.getText().toString(), now_date, now_time, "1", "");


    if(check>0){


        hr2 = spliteString(timeShow.getText().toString(), "日", "index", "back");
        year=Integer.parseInt(spliteString(timeShow.getText().toString(), "年", "index", "front"));
        month= Integer.parseInt(timeShow.getText().toString().substring(5,7));    
        day= Integer.parseInt(timeShow.getText().toString().substring(8,10));
        hr=Integer.parseInt(hr2.substring(1, hr2.indexOf(":")));
        min=Integer.parseInt(spliteString(hr2, ":", "index", "back"));


                c.setTimeInMillis(System.currentTimeMillis());
                  //c.set(year, month, day, hr, min, 0);
                c.set(Calendar.YEAR,year);
                c.set(Calendar.MONTH,month);
                c.set(Calendar.DATE,day);
                c.set(Calendar.HOUR_OF_DAY,hr);
                c.set(Calendar.MINUTE,min);
                c.set(Calendar.SECOND,0);
                c.set(Calendar.MILLISECOND,0);

           Intent intent = newIntent(getBaseContext(),CallAlarmText.class);
           PendingIntent sender=PendingIntent.getBroadcast(
              getBaseContext(),myCursor.getCount()+1, intent, PendingIntent.FLAG_ONE_SHOT);
                  AlarmManager am;
                  am = (AlarmManager)getSystemService(ALARM_SERVICE);
                  am.set(AlarmManager.RTC_WAKEUP,
                         c.getTimeInMillis(),
                         sender 
                        );
Log.d(now_time, "日期格式@@@@:"+year+"年"+month+"月"+day+"日"+hr+"時"+min);

         db.close();

        Toast.makeText(this, "新增成功", Toast.LENGTH_LONG).show();
        intent.setClass(Create_activity.this, NoteActivity.class);
        NoteActivity.NoteActivity.finish();
        startActivity(intent);
        Create_activity.this.finish();
    }else
    {
        Toast.makeText(this, "新增失敗", Toast.LENGTH_LONG).show();

    }
    date_db.close();
}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.notepad_remind"
android:installLocation="internalOnly"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >




    <activity
        android:name="com.notepad_remind.NoteActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


    <activity
        android:name="com.notepad_remind.Create_activity"
        android:label="@string/app_name"
        >
    </activity>

    <activity 
        android:name="com.notepad_remind.choose"
        android:label="@string/app_name" 
        android:theme="@android:style/Theme.Dialog"
        >

    </activity>

    <activity 
        android:name="com.notepad_remind.Note_content"
        android:label="@string/app_name" 
        >

    </activity>

    <activity 
        android:name="com.notepad_remind.Revise_activity"
        android:label="@string/app_name" 
        >

    </activity>

    <activity android:name=".AlarmAlert"
          android:label="@string/app_name">
    </activity>

    <receiver android:name=".CallAlarmText" android:process=":remote" >

        </receiver>
</application>

CallAlarmText.java

public class CallAlarmText extends BroadcastReceiver
 {
  @Override
   public void onReceive(Context context, Intent intent)
   {
    Intent i = new Intent(context, AlarmAlert.class);
    Log.d("CallAlarmText-text","日期格式@@@@:000000000000");
    Bundle bundleRet = new Bundle();
    bundleRet.putString("STR_CALLER", "");
    i.putExtras(bundleRet);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   context.startActivity(i);
    }
   }

AlarmAlert.java

public class AlarmAlert extends Activity
{
  @Override
  protected void onCreate(Bundle savedInstanceState) 
 {
     super.onCreate(savedInstanceState);
     new AlertDialog.Builder(AlarmAlert.this)
    .setIcon(R.drawable.clock)
    .setTitle("000!!")
    .setMessage("0000!!!")
    .setPositiveButton("000000",
     new DialogInterface.OnClickListener()
      {
         public void onClick(DialogInterface dialog, int whichButton)
          {
          AlarmAlert.this.finish();
          }
     })
      .show();
 } 

}

1 个答案:

答案 0 :(得分:0)

尝试用以下代码替换您的警报代码:

Intent intent = new Intent(getApplicationContext(), AlarmCheck.class);
    PendingIntent sender = PendingIntent.getBroadcast(getBaseContext(), 1,
            intent, 0);
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 5000, sender);