2015-07-09 43 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

我使用本地組件!是的,這是一個列表! =新班。佈局等等:-) –