警报管理器触发销毁

时间:2015-05-23 10:30:27

标签: android alarmmanager ondestroy

我正在为alarmmanager工作。 Alarmmanager按照我设置的时间触发。但问题是当系统或用户销毁应用程序时,alarmmanager再次触发。它只在用户设置了alarmmanager时触发。我的意思是在重新启动后如果用户打开应用程序并关闭而没有设置警报,它就不会在销毁时触发。

但是在设置之后,它会对所有破坏都开火。

这是我的代码:

清单

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        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=".timePicker"
        android:label="@string/title_activity_time_picker" >
    </activity>
    <service android:name=".MyAlarmService" />

    <receiver android:name=".MyAlarmReceiver"/>

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

TimePicker.class

public class timePicker extends ActionBarActivity {
private PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_time_picker);

    final TimePicker timePicker = (TimePicker)findViewById(R.id.timePicker);
    timePicker.setIs24HourView(true);



    Button tamam = (Button) findViewById(R.id.tamam);
    tamam.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            int hour = timePicker.getCurrentHour();
            int min = timePicker.getCurrentMinute();

            //Takvim tanımlaması
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());

            calendar.set(Calendar.HOUR_OF_DAY, hour);
            calendar.set(Calendar.MINUTE, min);
            calendar.set(Calendar.SECOND, 0);
            Intent myIntent = new Intent(timePicker.this, MyAlarmReceiver.class);
            pendingIntent = PendingIntent.getBroadcast(timePicker.this, 0, myIntent, 0);

            AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
            alarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),alarmManager.INTERVAL_DAY, pendingIntent);

            Intent i = new Intent(timePicker.this, MainActivity.class);
            Bundle bilgiGonder =new Bundle() ;
            bilgiGonder.putInt("hour",hour);
            bilgiGonder.putInt("min",min);
            i.putExtras(bilgiGonder);
            startActivity(i);
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_time_picker, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

MainActivty

public class MainActivity extends ActionBarActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    int hour,min;
    TextView time = (TextView) findViewById(R.id.time);
    Intent intent = getIntent();

    Bundle bilgiAl = intent.getExtras();
    if(bilgiAl != null) {

        hour = bilgiAl.getInt("hour");
        min = bilgiAl.getInt("min");

        time.setText(new StringBuilder().append(hour).append(" : ").append(min));



    }else{
        time.setText("Ayarlanmadi");
    }
    //Butona basınca dıger sayfayı cagırmak
    Button button_hatirlatici = (Button) findViewById(R.id.button_hatirlatici);
    button_hatirlatici.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Diger sayfamizi cagirmak icin yeni bir intent olustururuz
            Intent i = new Intent(MainActivity.this, timePicker.class);
            startActivity(i);
        }
    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

MyAlarmreceiver

public class MyAlarmReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent){
    //Bu receiver MainActivity de tanımladığımız alarm manager broadcast yaptığında bu sinyali yakalayıp MyAlarmService servisini
    //çalıştırır.MyAlarmService manifestte servis olarak tanımlanmıştır.
    //Myreciever de manifesto da reciever olarak tanımlanmıştır.
    Intent service1 = new Intent(context, MyAlarmService.class);
    context.startService(service1);
}

}

MyAlarmService(它会触发通知)

public class MyAlarmService extends Service {
 //private NotificationManager mManager;
public IBinder onBind(Intent arg0){
    return null;
}

public void onCreate(){
    super.onCreate();
}
public void onStart(Intent intent, int startId){
    super.onStart(intent, startId);

   Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    long[] pattern = {500,500,500,500,500,500,500,500,500};
    NotificationManager nm = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
    //Yeni bir pendingIntent açıyoruz ve içerisine de yeni bir intent oluşturuyoruz
    PendingIntent pi = PendingIntent.getActivity(MyAlarmService.this, 0, new Intent(MyAlarmService.this, MainActivity.class), 0);
    //Ardından notification adında bir notification yani bildirim oluşturuyoruz ve bilgilerini giriyoruz.
    Notification notification =new Notification.Builder(MyAlarmService.this)
            .setContentIntent(pi) // bildirime tıklayınca açılacak olan pendingintent
            .setContentTitle("Bildirim Ornegi")
            .setContentText("Bildirimin içindeki text")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setAutoCancel(true) // Bildirime tıklayınca bildirim yok olsun.
            .setSound(alarmSound)
            .setVibrate(pattern)
            .setLights(Color.BLUE,500,500)
            .addAction(R.mipmap.ic_launcher, "bildirimin en alt mesajı", pi) // bildirimi açınca altta bişeyler daha çıkarıyor.
            .build(); //Butun yukarda girilen özellikleri combine edip yeni notification objesi oluştur.

    nm.notify(1, notification);

有人可以帮忙吗?

0 个答案:

没有答案