Java 在android中如何将视图作为参数传递?
我想在我的android应用程序中创建每日通知。为此,我创建了一个Java 在android中如何将视图作为参数传递?,java,android,Java,Android,我想在我的android应用程序中创建每日通知。为此,我创建了一个createNotification方法。现在,我想在特定时间调用该方法。我的mainactivity.java如下所示: package com.example.shiza.dailyquranquote; import android.content.Intent; import android.content.SharedPreferences; import android.support.v7.app.ActionBa
createNotification
方法。现在,我想在特定时间调用该方法。我的mainactivity.java
如下所示:
package com.example.shiza.dailyquranquote;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import java.util.Calendar;
import java.util.Date;
public class MainActivity extends ActionBarActivity {
Date startDate= new Date("03/31/2015 12:00:00");
Date endDate = new Date();
TextView textView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.verse);
long startDay = startDate.getTime() / 1000 / 60 / 60 / 24;
long endDay = endDate.getTime() / 1000 / 60 / 60 / 24;
long daysBetween = endDay - startDay;
SharedPreferences sharedPreferences = getSharedPreferences("QuranVerse",0);
String id = ""+ (daysBetween % 365) ;
String verse = sharedPreferences.getString(id, "");
if (verse == null)
{
verse ="Sufficient is Allah for me; There is no power except Him; And in Him I put my trust.";
}
Calendar rightNow = Calendar.getInstance();
textView.setText(verse + "current hour is" + rightNow.HOUR_OF_DAY);
if ( rightNow.HOUR_OF_DAY == 11 )
{
createNotification();
}
}
public void goToInsertVerse(View v)
{
Intent intent = new Intent(this,InsertVerse.class);
startActivity(intent);
}
public void goToGetVerse(View v)
{
Intent intent = new Intent(this,GetVerse.class);
startActivity(intent);
}
public void createNotification(View view) {
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// Build notification
// Actions are just fake
Notification noti = new Notification.Builder(this)
.setContentTitle("New mail from " + "test@gmail.com")
.setContentText("Subject").setSmallIcon(R.drawable.background)
.setContentIntent(pIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
}
现在我需要将视图作为参数传递给
createNotification
。我做不到。如何实现它?以“”为参数的用户方法为什么要传递视图?您的困难是什么?您可以像传递任何其他参数一样传递它。因为createNotification需要此参数。在我的方法中不是必需的吗?@Gabeschen在主活动中获取错误createNotification(view)无法应用。您将方法定义为public void createNotification(view view)
,但在实际使用view
参数的方法中,您什么也不做……那么,为什么要这样定义它呢?将方法定义更改为public void createNotification()
,它将解决此问题。