2015-07-09 55 views
0

我正在建设一个节日应用程序,在那里你可以得到特定音乐会的提醒。我不知道如何做到这一点,所以我真的希望有人能帮助我。我已经制作了开关和布局。我只需要开关来激活警报。使开关设置一个预定义的闹钟

下面是我的一些代码:

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

你可以做提醒使用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); 
    } 

} 
+0

嗯,也许愚蠢qestion但我应该在哪里把这个代码! //当需要提醒用户时,AlarmReceiver将被调用。 //并使用putExtra您可以保存音乐会数据,例如音乐会的名字。 Intent intent = new Intent(mContext,AlarmReceiver.class); 等等:) 和hwo我做一个开关激活BroadcastReciver? :-) 谢谢。 –

+0

你只是使用webview来查看内容,或者你会使用某种本地组件,比如List? – heloisasim

+0

我使用本地组件!是的,这是一个列表! =新班。布局等等:-) –