0

Android アプリを開発しています。1 日あたり 5 つの通知を提供したいと考えています。通知時間を sqlite データベースに保存します。次に、サービスを使用して繰り返しループでこのデータベース通知時間をデバイスの時間と比較します。しかし、通知は機能しません。 Forceclose が表示されました。Third、java、および MyService の 2 つのクラス ファイルを以下に示しました.......助けが必要です

     Third.java

   public class third extends Activity {
/** Called when the activity is first created. */
Button btnopen;
Spinner spin1,spin2,spin3,spin4,spin5;
EditText t1,t2,t3,t4,t5;
String fajr,zuhr,asr,magrib,isha;
String[] item={"","2-Times Allahu akbar","3-Beep once"};
example r=new example();
private static final String DATABASE_NAME = "MYPRAYER.db";   
private static final String DATABASE_TABLE = "notification";
private static final String DATABASE_CREATE = "create table "+DATABASE_TABLE+"(id integer primary key autoincrement , fajr varchar not null , zuhr varchar not null , asr varchar not null , magrib varchar not null , isha varchar not null );"; 

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page3);
   /* Call the service */
    Intent i = new Intent();
    i.setClassName( "org.prayer.test","org.prayer.test.MyService" );
    bindService( i, null, Context.BIND_AUTO_CREATE);
    this.startService(i); 
    /*  Populate the spinner */
btnopen=(Button)findViewById(R.id.button1);
t1=(EditText)findViewById(R.id.editText1);
t2=(EditText)findViewById(R.id.editText2);
t3=(EditText)findViewById(R.id.editText3);
t4=(EditText)findViewById(R.id.editText4);
t5=(EditText)findViewById(R.id.editText5);
spin1=(Spinner)findViewById(R.id.spinner1);
spin2=(Spinner)findViewById(R.id.spinner3);
spin3=(Spinner)findViewById(R.id.spinner4);
spin4=(Spinner)findViewById(R.id.spinner6);
spin5=(Spinner)findViewById(R.id.spinner7);
ArrayAdapter<String> a1=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,item);
a1.setDropDownViewResource(android.R.layout.simple_spinner_item);
spin1.setAdapter(a1);
spin2.setAdapter(a1);
spin3.setAdapter(a1);
spin4.setAdapter(a1);
spin5.setAdapter(a1);

         /* Notification time stored on the database */
try
{
        SQLiteDatabase myDB;
        //myDB = openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);
        myDB = SQLiteDatabase.openDatabase("/data/data/org.prayer.test/databases/MYPRAYER.db",null,SQLiteDatabase.CREATE_IF_NECESSARY);

        //myDB.execSQL(DATABASE_CREATE);
        myDB.execSQL(DATABASE_CREATE);
        myDB = openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);
        ContentValues newRow = new ContentValues();
        newRow.put("id", "1");
        newRow.put("fajr", "0:00 AM");
        newRow.put("zuhr", "0:00 AM");
        newRow.put("asr", "0:00 AM");
        newRow.put("magrib", "0:00 AM");
        newRow.put("isha", "0:00 AM");
        myDB.insert(DATABASE_TABLE, null, newRow);
        myDB.close();
}
catch(SQLiteException ex)
{
    //Do nothing
    //System.out.println(ex.getMessage());
}

btnopen.setOnClickListener(new OnClickListener() {
      @Override
    public void onClick(View v) {

         fajr=t1.getText().toString();
         zuhr=t2.getText().toString();
         asr=t3.getText().toString();
         magrib=t4.getText().toString();
         isha=t5.getText().toString();

        SQLiteDatabase myDB;
        myDB = openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);
        ContentValues newRow = new ContentValues();
        newRow.put("id", "1");
        newRow.put("fajr", fajr);
        newRow.put("zuhr", zuhr);
        newRow.put("asr", asr);
        newRow.put("magrib", magrib);
        newRow.put("isha", isha);
        myDB.update(DATABASE_TABLE, newRow,"id" + "=" + "1",null);
        Toast.makeText(getApplicationContext(), "row updated", Toast.LENGTH_SHORT).show();
        myDB.close();
  }

});

} }

MyService.java

ppublic class MyService extends Service {
              private static final String DATABASE_NAME = "MYPRAYER.db";   
              private static final String DATABASE_TABLE = "notification";
              String tag="TestService";
                String a="hello";
                String res1,res2,res3,res4,res5;
                int hour,minute;
                String day3,day4,minute1,hour1;
                MediaPlayer mMediaPlayer;
                   /* Service creation */
               @Override
               public void onCreate() {
             super.onCreate();
               Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();      
               Log.i(tag, "Service created...");


                  /* Rereive notification times from the databse notificaion */
              SQLiteDatabase myDB;
            myDB = openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);
            String[] resultColumns = new String[] {"fajr","zuhr","asr","magrib","isha"};
            Cursor allRows = myDB.query(DATABASE_TABLE, resultColumns, null, null, null, null, null, null);

            Integer cindex = allRows.getColumnIndex("fajr");
            Integer cindex1 = allRows.getColumnIndex("zuhr");
            Integer cindex2 = allRows.getColumnIndex("asr");
            Integer cindex3 = allRows.getColumnIndex("magrib");
            Integer cindex4 = allRows.getColumnIndex("isha");
            allRows.moveToFirst();
            res1=allRows.getString(cindex);
            res2=allRows.getString(cindex1);
            res3=allRows.getString(cindex2);
            res4=allRows.getString(cindex3);
            res5=allRows.getString(cindex4);


            myDB.close();

            while(!Thread.currentThread().isInterrupted())
            {
            try 
            {

                Calendar cal = Calendar.getInstance(); // Rereive calender date
                hour = cal.get(Calendar.HOUR_OF_DAY); // Take hour from the calender
                minute = cal.get(Calendar.MINUTE);    // Take minute from the calender
                minute1=minute + "";  // Convert minute to dtring
                if(minute1.equals("1")) 
                {
                minute1="01";
                }
                else if(minute1.equals("2"))
                {
                minute1="02";
                }
                else if(minute1.equals("3"))
                {
                minute1="03";
                }
                else if(minute1.equals("4"))
                {
                minute1="04";
                }
                else if(minute1.equals("5"))
                {
                minute1="05";
                }
                else if(minute1.equals("6"))
                {
                minute1="06";
                }
                else if(minute1.equals("7"))
                {
                minute1="07";
                }
                else if(minute1.equals("8"))
                {
                minute1="08";
                }
                else if(minute1.equals("9"))
                {
                minute1="09";
                }
                     /* Converting to 12 hour format */
                if ( hour < 12 )
                {
                     hour=hour;
                     day3=hour + "" + ":" + minute1;
                     day4=day3 + " " + "AM"; // DAY4 Contains the system time
                }
                else
                {
                    hour=hour-12;
                    day3=hour + "" + ":" + minute1;
                    day4=day3 + " " + "PM";
                }



                if(day4.equals(res1))
              {
                     Notification("Notification Title","Notification Message");


              }



                 if(day4.equals(res2))
                  {
                         Notification("Notification Title","Notification Message");

                  }


                 if(day4.equals(res3))
                  {
                         Notification("Notification Title","Notification Message");

                  } 
                 if(day4.equals(res4))
                  {
                         Notification("Notification Title","Notification Message");

                  }
                 if(day4.equals(res5))
                  {
                         Notification("Notification Title","Notification Message");

                  }

               Thread.sleep(1000);
       } 
       catch (InterruptedException e)
       {
               Thread.currentThread().interrupt();
               //System.out.println("New 1 Exception here");

       }

       catch (Exception e)
      {
               //Thread.currentThread().interrupt();
           System.out.println("nEW Exception here");
      }          
       }

       }         
          @Override
          public void onStart(Intent intent, int startId) {      
          super.onStart(intent, startId);  
          Log.i(tag, "Service started...");
       }
         @Override
         public void onDestroy() {
         super.onDestroy();
         Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
       }

         @Override
         public IBinder onBind(Intent intent) {
         return null;
        }

         private  void Notification(String notificationTitle, String notificationMessage)
       {
         NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
         Notification notification = new Notification(R.drawable.ic_launcher, "A New Message!", System.currentTimeMillis());

         Intent notificationIntent = new Intent(this,MyService.class);
         PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

         notification.setLatestEventInfo(MyService.this, notificationTitle, notificationMessage, pendingIntent);
         notificationManager.notify(10001, notification);
   //HELLO_ID++;
         Uri alert = RingtoneManager.getDefaultUri(
        RingtoneManager.TYPE_NOTIFICATION); 

        mMediaPlayer = new MediaPlayer();
      try
      {
      mMediaPlayer.setDataSource(this, alert);

      AudioManager audioManager = (AudioManager)getSystemService(
                                  Context.AUDIO_SERVICE);
      int volumen = audioManager.getStreamVolume(
                    AudioManager.STREAM_NOTIFICATION);

      if (volumen != 0) {
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
        mMediaPlayer.setLooping(true);
        mMediaPlayer.prepare();
        mMediaPlayer.start();
      }
      }
      catch(IOException e)
      {  
          System.out.println("Exception here");
          //System.out.println("Exception 1 here");
      }
      }
          }

ラグキャット:

07-27 16:24:52.591: E/global(363): 非推奨のスレッド メソッドはサポートされていません。07-27 16:24:52.591: E/global(363): java.lang.UnsupportedOperationException 07-27 16:24:52.591: E/global(363): java.lang.VMThread.stop(VMThread.java: 85) 07-27 16:24:52.591: E/グローバル (363): java.lang.Thread.stop(Thread.java:1379) で 07-27 16:24:52.591: E/グローバル (363): でjava.lang.Thread.stop(Thread.java:1344) 07-27 16:24:52.591: E/global(363): org.prayer.test.splash$1.run(splash.java:71) で 07- 27 16:24:53.002: D/dalvikvm(363): GC は 879 オブジェクト / 65552 バイトを 108 ミリ秒で解放しました 07-27 16:25:00.542: I/TestService(363): サービスが作成されました... 07-27 16:25 :20.752: I/dalvikvm(363): threadid=7: シグナル 3 07-27 16:25:20.803 に反応しています: I/dalvikvm(363): スタック トレースを「/data/anr/traces.txt」に書き込みました

4

1 に答える 1

0

1日の決まった時間に通知をトリガーすることが目的の場合は、AlarmManagerを使用して次のようにします。

  1. アプリケーションを初めて起動するときは、現在行っているようにこれらのEditText値などを保存します。次に、AlarmManagerを設定して、最も近い次の通知時間にサービスをウェイクアップします。
  2. サービスで、通知を投稿し、次のアラームを設定して、stopSelf()を実行します。

このサイクルは続きます。

強制終了については、logcatを貼り付けてください。あなたのコードはあまりきちんとしておらず、変数は意味のある名前が付けられていないので、わかりにくいです。

于 2012-07-23T19:16:54.993 に答える