2016-06-16 7 views
1

私は多くを検索しましたが、私の特定の問題の答えを見つけることができなかったので、私自身の質問をしています。Alarmobjectを作成するとアプリケーションがクラッシュする - Alarmmanager

問題:

私は、ユーザーが別の繰り返し間隔で複数のアラームを設定することができ、アプリケーションをプログラミングしています。しかし、Alarmオブジェクトを作成すると、アプリがクラッシュします。

Alarmクラスを使用して呼び出され、ユーザーがボタンをクリックするたびに:これは私のAlarmクラス

Alarm b = new Alarm(name, intervall, 23, 30, 15, 6, 2016); 

です:

package witty.de.witty; 

import android.app.Activity; 
import android.app.AlarmManager; 
import android.app.PendingIntent; 
import android.app.Service; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.IBinder; 
import java.util.Calendar; 

public class Alarm extends Activity { 
    public Alarm(String name, int intervall, int stunden, int minuten, int tag, int monat, int jahr) { 
     Calendar cal = Calendar.getInstance(); 
     cal.set(Calendar.YEAR, jahr); 
     cal.set(Calendar.MONTH, (monat - 1)); 
     cal.set(Calendar.DAY_OF_MONTH, tag); 
     cal.set(Calendar.HOUR_OF_DAY, stunden); 
     cal.set(Calendar.MINUTE, minuten); 
     cal.set(Calendar.SECOND, 0); 

     Intent myIntent = new Intent(Alarm.this, AlarmReceiver.class); 

     final int _id = (int) System.currentTimeMillis(); 
     myIntent.setData(Uri.parse("reminder:" + _id)); 

     PendingIntent pendingIntent = PendingIntent.getBroadcast(this, _id, myIntent, PendingIntent.FLAG_ONE_SHOT); 

     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

     alarmManager.setRepeating(AlarmManager.RTC, cal.getTimeInMillis(), intervall * AlarmManager.INTERVAL_DAY, 
       pendingIntent); 
    } 
} 

が、これは私のNotificationServiceクラスです:

package witty.de.witty; 

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.support.v4.app.NotificationCompat; 
import android.widget.Toast; 

import java.util.logging.Logger; 

import witty.de.witty.MainActivity; 
import witty.de.witty.R; 

public class NotificationService extends Service { 
    private NotificationManager mManager; 

    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 
     super.onStart(intent, startId); 

     Toast mToast = Toast.makeText(
       this, 
       "Reminders added to the calendar successfully for ", 
       Toast.LENGTH_LONG); 
     mToast.show(); 

     // Getting Notification Service 
     mManager = (NotificationManager) this.getApplicationContext() 
       .getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE); 

     Intent intent1 = new Intent(this.getApplicationContext(), 
       MainActivity.class); 

     intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP 
       | Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
       this.getApplicationContext(), 0, intent1, 
       PendingIntent.FLAG_UPDATE_CURRENT); 

     NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(this) 
         .setSmallIcon(R.mipmap.ic_launcher) 
         .setContentTitle("Witty2") 
         .setContentIntent(pendingNotificationIntent) 
         .setContentText("Errinerung!!"); 

     mManager.notify(0, mBuilder.build()); 
    } 

    @Override 
    public void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
    } 
} 

これは私のですクラス:

package witty.de.witty; 

import android.app.NotificationManager; 
import android.content.Context; 
import android.support.v4.app.NotificationCompat; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 

public class AlarmReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Intent myIntent = new Intent(context, NotificationService.class); 
     context.startService(myIntent); 
    } 
} 

そして、これは私のAndroidManifestファイルです:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="witty.de.witty"> 

    <uses-permission android:name="android.permission.WAKE_LOCK" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".Erstellen"></activity> 
     <activity android:name=".Impressum"></activity> 
     <activity android:name=".Startseite"></activity> 
     <service android:name=".NotificationService"></service> 
     <receiver android:name=".AlarmReceiver" android:process=":remote"></receiver> 
     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 
    </application> 

</manifest> 

あなたはより多くのコードやより多くの情報が必要な場合は、ちょうど私に尋ねます。

+2

クラッシュ出力を投稿してください。 –

答えて

0

私のソリューション: はないObjectとして、新たな意図を持ってAlarmクラスを呼び出します。

Intent intent = new Intent(this, Alarm.class); 
startActivity(intent); 
関連する問題