简体   繁体   English

有人可以帮我自动创建闹钟吗?

[英]Someone can help me the create a alarm android automatic?

I need create a code of notifications with alarm for android that meets the requirements:我需要为符合要求的 android 创建带有警报的通知代码:

  • The APP must provide a selector of STARTTIME and a ENDTIME of your workday.(Because I need to calculate the difference between the hours for generate the TOTALTIME ) APP 必须提供您工作日的STARTTIMEENDTIME选择器。(因为我需要计算生成TOTALTIME的小时数之间的TOTALTIME
  • The notification (the Alarm) should have a status of active or inactive.(For the alarm don't ringing without permission.)通知(闹钟)应具有活动或非活动状态。(对于未经许可的闹钟,请勿响铃。)
  • The notification (the Alarm) should continue working till when the cell phone is off equal to "Broadcast at the Eclipse or Android Studio"通知(警报)应该继续工作,直到手机关闭等于“在 Eclipse 或 Android Studio 上广播”
  • The notification (the Alarm) only should start of accord with the STARTTIME selected.通知(警报)仅应根据所选的STARTTIME开始。

Now i need that it working with the following conditions when is active:现在我需要它在活动时在以下条件下工作:

  • The ALARM1 variable should do a notification when achieve "<5% of variable TOTALTIME ", after that the TOTALTIME achieve ">=5%" the variable ALARM2 should get in loop, notifying me of 2 in 2 hours till the TOTALTIME achieve "97%" and for last, i need that the variable ALARM3 show me a last notification when the TOTALTIME achieve ">=97%".当达到“<5% of variable TOTALTIME ”时, ALARM1变量应该做一个通知,之后TOTALTIME达到“>=5%”变量ALARM2应该进入循环,在 2 小时内通知我 2 直到TOTALTIME达到“97 %",最后,我需要变量ALARM3TOTALTIME达到 ">=97%" 时向我显示最后一个通知。

I got it!我知道了!


MainActivity.java主活动.java

package br.exemploalarmmanagerbn;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        boolean alarmeAtivo = (PendingIntent.getBroadcast(this, 0, new Intent("ALARME_DISPARADO"), PendingIntent.FLAG_NO_CREATE) == null);  
        if(alarmeAtivo){
            Log.i("Script", "Novo alarme");
            Intent intent = new Intent("ALARME_DISPARADO");
            PendingIntent p = PendingIntent.getBroadcast(this, 0, intent, 0);
            Calendar c = Calendar.getInstance();
            c.setTimeInMillis(System.currentTimeMillis());
            c.add(Calendar.SECOND, 3);
            AlarmManager alarme = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarme.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), 5000, p);
        }
        else{
            Log.i("Script", "Alarme já ativo");
        }
    }
    @Override
    public void onDestroy(){
        super.onDestroy();
        /*Intent intent = new Intent("ALARME_DISPARADO");
        PendingIntent p = PendingIntent.getBroadcast(this, 0, intent, 0);
        AlarmManager alarme = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarme.cancel(p);*/
    }
}

BroadcastReceiverAux.java广播接收器Aux.java

package br.exemploalarmmanagerbn;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
public class BroadcastReceiverAux extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) { 
        Log.i("Script", "-> Alarme");
        gerarNotificacao(context, new Intent(context, MainActivity.class), "Nova mensagem", "Título", "Descrição nova mensagem");
    }
    public void gerarNotificacao(Context context, Intent intent, CharSequence ticker, CharSequence titulo, CharSequence descricao){
        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        PendingIntent p = PendingIntent.getActivity(context, 0, intent, 0); 
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setTicker(ticker);
        builder.setContentTitle(titulo);
        builder.setContentText(descricao);
        builder.setSmallIcon(R.drawable.ic_launcher);
    builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher));
        builder.setContentIntent(p);    
        Notification n = builder.build();
        n.vibrate = new long[]{150, 300, 150, 600};
        n.flags = Notification.FLAG_AUTO_CANCEL;
        nm.notify(R.drawable.ic_launcher, n);
        try{
            Uri som = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Ringtone toque = RingtoneManager.getRingtone(context, som);
            toque.play();
        }
        catch(Exception e){}
    }
}

activity_main.xml活动_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Exemplo AlarmManager" />
</RelativeLayout>

AndroidManifest.xml AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.VIBRATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="br.exemploalarmmanagerbn.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>
        <receiver android:name="BroadcastReceiverAux" android:label="BroadcastReceiverAux">
            <intent-filter>
                <action android:name="ALARME_DISPARADO"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

Sorry me is my first time in a forum, still don't know how act in him.对不起我是我第一次上论坛,还不知道他的表现如何。

I got so far these codes.到目前为止,我得到了这些代码。 The problem are conditions of code, how do i do these conditions.问题是代码的条件,我该怎么做这些条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM