簡體   English   中英

如何在Android中將視圖作為參數傳遞?

[英]How can I pass view as parameter in android?

我想每天在我的Android應用程序中創建通知。 為此,我做了一個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);

    }
}

現在,我需要將view作為參數傳遞給createNotification 我做不到。 我該如何實現?

用戶setContent方法將“ RemoteViews ”作為參數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM