Soru & Cevap

Alarm manager ...

07.05.2015 - 10:39

Arkadaşlar 10 sn' de bir çalışacak alarm manager oluşturmaya çalışıyorum.Fakat alarm manager çalışmıyor.Sıkıntı nedir acaba...

Uygulamadaki oncreat altına şu kodları attım

        Intent dialogIntent = new Intent(getBaseContext(), NotificationReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, dialogIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
        alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),1000*10, pendingIntent);

10 sn'de bir tekrarlanacak satırlar....

public class NotificationReceiver extends BroadcastReceiver {

            @Override
            public void onReceive(Context context, Intent intent) {
                
                 uartInterface.SetConfig(57600, (byte) 8, (byte) 1, (byte) 0, (byte) 0); 
                   srcStr="AA 55 35 12 10";
                  WriteData(srcStr);
                   uartInterface.SendData(6, writeBuffer);
            }

        }

    Manifest 'de yaptığım değişiklikleride ekliyorum.....

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

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="16" />

    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:largeHeap="true" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:configChanges="orientation|screenSize"
             >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="com.uygulama2.NotificationReceiver"></receiver>
        
    </application>

</manifest>

67 Görüntülenme

2 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

picture-1372-1408467635.jpg
ahmtbrk
07.05.2015 - 02:48

Uygulama açıkken mi 10 saniyede bir calısacak?

Eğer öyle ise Timer veya Handler kullanmanı tavsiye ederim.

AlarmManager backgroundda ki uzun vadeli işlemler için kullanılmalı.

Profile picture for user green_day
green_day
07.05.2015 - 02:28

 

 

Böyle kullanmayı deneneyin bide

public class MainActivity extends Activity
{
 
    private PendingIntent pendingIntent;
    
    @Override
    public void onCreate(Bundle savedInstanceState)
     {
      
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
    
    
      Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
      pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
    
      AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
      alarmManager.set(AlarmManager.RTC, System.currentTimeMillis(), 1000*10 , pendingIntent);
   
    }
}


public class MyReceiver extends BroadcastReceiver
{
     
    @Override
    public void onReceive(Context context, Intent intent)
    {
 //servisi tetiklesin
       Intent service1 = new Intent(context, MyAlarmService.class);
       context.startService(service1);
       
    }  
}

//Bildirinm düşecek olan serisin de bu
public class MyAlarmService extends Service
{
     
   private NotificationManager mManager;
 
    @Override
    public IBinder onBind(Intent arg0)
    {
 
        return null;
    }
 
    @Override
    public void onCreate()
    {
       // TODO Auto-generated method stub 
       super.onCreate();
    }
 
   @Override
   public void onStart(Intent intent, int startId)
   {
 //Servis basladığında gerçeklesecekler...

       super.onStart(intent, startId);
     
       mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
       Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);
    
       Notification notification = new Notification(R.drawable.ic_launcher,"Mesaj", System.currentTimeMillis());
       intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
 
       PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
       notification.flags |= Notification.FLAG_AUTO_CANCEL;
       notification.setLatestEventInfo(this.getApplicationContext(), "Geleceği yazanlar...", "Bu mesaj geleceği yazanlardan!", pendingNotificationIntent);
 
       mManager.notify(0, notification);
    }
 
    @Override
    public void onDestroy()
    {
  //servisi sonlandırma
        super.onDestroy();
    }
 
}