使开关设置为预定义的警报

时间:2015-07-09 21:20:16

标签: java android

我正在制作一个节日应用程序,您可以在其中获取特定音乐会的提醒。我不知道怎么做,所以我真的希望有人可以帮助我。我已经制作了开关和布局。我只需要开关来激活警报。

以下是我的一些代码:

package com.example.kjart.borkmusik;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {

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



        WebView myWebView = (WebView) findViewById(R.id.Webview);
        myWebView.loadUrl("http://www.borkhavnmusikfestival.dk/");
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
    }

    @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){
        switch(item.getItemId()){
            case R.id.action_settings:
                Intent intent = new Intent();
                Intent launchNewIntent = new Intent(MainActivity.this,Pamindelse.class);
                startActivityForResult(launchNewIntent, 0);
                return true;
        }
        return false;
    }
}


package com.example.kjart.borkmusik;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class Pamindelse extends ActionBarActivity {

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

    @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_pamindelse, 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.layout.activity_pamindelse) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}


package com.example.kjart.borkmusik;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class Pamindelse extends ActionBarActivity {

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

    @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_pamindelse, 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.layout.activity_pamindelse) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用BroadcastsReceivers进行提醒,并且您可以在音乐会即将开始时收到推送通知。

// The AlarmReceiver will be called when the user need to be reminded. 
// and using putExtra you can save the concert data, e.g. the concert name.
Intent intent = new Intent(mContext, AlarmReceiver.class);
String[] texts = new String[4];
texts[0] = CONCERT_NAME;
intent.putExtra("alarm_message", texts);

PendingIntent sender = PendingIntent.getBroadcast(mContext, CONCERT_ID, intent, 0);

// Create an alarm manager
AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, CONCERT_TIME_IN_MILLIS, sender);

AlarmReceiver.class

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

            // Retrieve the concert data, e.g. the concert title
            String[] message = intent.getStringArrayExtra("alarm_message");
            CharSequence concertTitle = message[0];
            CharSequence messageText = "The concert is starting";
            long when = System.currentTimeMillis();

            NotificationManager mNotificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);

            Notification notification = new Notification(R.drawable.ic_launcher, concertTitle, when);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;

// This will open the activity that will be open then clicked on the push notification
            Intent i = new Intent(context, MainActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            PendingIntent pendingIntent = PendingIntent.getActivity(context, (int) when, i, PendingIntent.FLAG_ONE_SHOT);
            notification.setLatestEventInfo(context, concertTitle, messageText, pendingIntent);

            mNotificationManager.notify(1, notification);
    }

}